MySQL Select into Multiple Variables Doesn't Work - mysql

delimiter //
drop function if exists get_rating;
create function get_rating(id tinyint(1) unsigned)
returns tinyint(1)
begin
declare rating int unsigned;
declare rating_count int unsigned;
select sum(rating), count(rating)
into rating, rating_count
from comments
where review_id = id;
return rating_count;
end //
delimiter ;
When I call the function, neither rating nor rating_count returns the right value (3) they both return 0 when it should be adding three fields with values of 1. Any ideas why this doesn't work?

There would be ambiguity on names of columns and local variables when they match by name.
You declared rating as variable but counting on a field with same name rating and assigning to again rating. It was causing an ambiguity.
Maintain them different.
Change your code as below:
delimiter //
drop function if exists get_rating //
create function
get_rating( _id tinyint(1) unsigned )
returns tinyint(1)
begin
declare _rating int unsigned;
declare _rating_count int unsigned;
select sum(rating), count(rating)
into _rating, _rating_count
from comments
where review_id = _id;
return _rating_count;
end;
//
delimiter ;

Related

stored procudure variaibles in mysql

I am declaring variables inside the procedure and setting those values to result from another query.when executed it is giving null.
version:8.0.16
call putrequest('x',"jiraUPM","ASE-12345","inprogress","testcybsjira.com");
requestId and reqid are not null.but it taking null value .
create procedure `putrequest`(in `employeeId` varchar(15),in `reqtype`
varchar(15),in `ticketId` varchar(15),in `status` varchar(15),in `details`
varchar(100))
begin
declare `rid` int;
declare `reqType` int;
select `requestId` into `reqType` from `requesttype` where `request`=`reqtype`;
select `reqId` into `rid` from `employee` where `empId`=`employeeId`;
insert into `requests` values(rid,reqType,`ticketId`,NOW(),NOW(),`status`,`details`);
end
Error executing SQL statement. Column 'reqId' cannot be null - Connection: Connection 1: 93ms
If either of the SELECT queries doens't find a matching row, the corresponding variable will be NULL, and you'll get an error when you try to insert it. You need to check for that before doing the INSERT.
But there's no need for separate SELECT queries and variables, use INSERT INTO ... SELECT ...
INSERT INTO requests
SELECT e.reqId, r.requestId, ticketId, NOW(), NOW(), status, details
FROM requesttype AS r
CROSS JOIN employee AS e
WHERE r.request = reqtype
AND e.empId = employeeId
If reqtype or employeeId can't be found, the join won't return any rows, so nothing will be inserted.
It creates a problem because of the same variable name "reqtype" (declare as a procedure parameter) and "reqType" (Declare as procedure variable)
It considers the null value for reqtype and not return any record.
MySQL is case insensitive . Be careful while given variable name.
DELIMITER $$
create procedure `putrequest`(in `employeeId` varchar(50),in `rtype`
varchar(50),in `ticketId` varchar(15),in `status` varchar(15),in `details`
varchar(100))
begin
declare `rid` int;
declare `reqType` int;
select `reqId` into `rid` from `employee` where `empId`=`employeeId`;
select `requestId` into `reqType` from requesttype where `request`=`rtype`;
insert into `requests` values(rid,reqType,`ticketId`,NOW(),NOW(),`status`,`details`);
end$$
DELIMITER ;
DEMO

MySQL optimize comparing function

I'm working on an Android program that introduces in a table approximately 15000 integer values(somewhere between 350-500 lines with 32 columns). In the DB I also have other similar values. This 15000 values that I'm talking about represent a processed image, so basically I want to compare the similarity of two images. Now, when I try to compare the values of two images(I'm comparing value by value and count the equal ones), only the data writing process takes about 7 minutes, which is way too long(I want to be able to write and compare at least 5 images in that time). I know that usually you don't work with this kind of things directly in the DB, but do you think that there is anything that I can do, or is it necessary to do this comparison on the server? The values returned by the descriptor came as line elements separated by ',' and each line is separated by ';'. I take each returned element and save it in a tables column. Here is my code:
Split function:
CREATE DEFINER=`root`#`localhost` FUNCTION `strSplit`(textIn longtext, delim varchar(12), count int) RETURNS int(11)
BEGIN
declare splitString INT(11);
SET splitString = replace(substring(substring_index(textIn, delim, count), length(substring_index(textIn, delim, count - 1)) + 1), delim, '');
RETURN splitString;
END
The function that creates the table:
CREATE TABLE IF NOT EXISTS `myguide`.`objectlocation` (
`ObjectLocationId` INT(11) NOT NULL AUTO_INCREMENT,
`ValueObject` LONGTEXT NOT NULL,
`DescriptorSize` INT(11) NOT NULL,
`DescriptionObject` VARCHAR(45) NOT NULL,
`DataInsert` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`InsertBy` VARCHAR(45) NULL DEFAULT NULL,
PRIMARY KEY (`ObjectLocationId`))
ENGINE = InnoDB
AUTO_INCREMENT = 2
DEFAULT CHARACTER SET = utf8
And this is the code that does the insert part:
CREATE DEFINER=`root`#`localhost` PROCEDURE `myguide_sp_info_imageId`(descriptorIn longtext, sizeDescriptor INT)
BEGIN
declare sizeImagesTable INT DEFAULT (select count(*) from objectLocation);
declare descriptorSizeImage INT;
declare descriptor INT;
declare sizeDescriptorImage INT DEFAULT sizeDescriptor;
declare contorInsertImage INT default 1;
declare descriptorForSplit longtext;
declare descriptorImageSaved longtext;
declare descriptorForSplitImageSaved longtext;
/* check if table exist, drop*/
DROP TEMPORARY TABLE IF EXISTS backupObjectLocation;
/* Create temporar table for store info about objectLocation*/
CREATE TEMPORARY TABLE backupObjectLocation (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
idImage int NOT NULL,
descriptorSaved longtext not null,
sizeDescriptorSaved float not null
);
/* check if table exist, drop*/
DROP TEMPORARY TABLE IF EXISTS processImage;
/* Create temporar table for store info about objectLocation*/
CREATE TEMPORARY TABLE processImage (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
descriptorSaved varchar(255) ,
descriptorReceived varchar(255)
);
SET descriptorImageSaved = RTRIM(descriptorIn);
SET descriptorForSplit = REPLACE(descriptorImageSaved, ';', ',');
INSERT INTO backupObjectLocation (idImage, descriptorSaved, sizeDescriptorSaved)
SELECT ObjectLocationId, ValueObject, DescriptorSize FROM objectLocation;
loop_insertDescriptorImage: LOOP
if contorInsertImage > sizeDescriptorImage then
leave loop_insertDescriptorImage;
end if;
SET descriptor = strSplit(descriptorForSplit, ',', contorInsertImage);
INSERT INTO processImage (descriptorReceived) VALUES (descriptor);
SET contorInsertImage = contorInsertImage + 1;
ITERATE loop_insertDescriptorImage;
end LOOP;
loop_table: LOOP
if sizeImagesTable > 1 then
leave loop_table;
end if;
SET descriptorSizeImage = (SELECT sizeDescriptorSaved from backupObjectLocation where id = sizeImagesTable);
loop_image: LOOP
if descriptorSizeImage > 1 then
leave loop_image;
end if;
SET descriptorImageSaved = (SELECT descriptorSaved from backupObjectLocation where id = sizeImagesTable);
SET descriptorForSplitImageSaved = REPLACE(descriptorImageSaved, ';', ',');
SET descriptorSizeImage = descriptorSizeImage + 1;
ITERATE loop_image;
end LOOP;
SET sizeImagesTable = sizeImagesTable + 1;
ITERATE loop_table;
end LOOP;
select descriptorImageSaved;
select * from backupObjectLocation;
select * from processImage;
END
Please help me find a solution.

Create Table and Insert using stored Procedure

I'am trying to create a procedure checking table and insert
but it show me some error which i'am not sure how to fix it
this is the error code
Explicit or implicit commit is not allowed in stored function or
trigger
DELIMITER ;;
CREATE FUNCTION `getLabel`(paradocid INT, paradoctype char(10),paradoclineid INT,paraqty INT,paracreated date,paracreatedby INT) RETURNS int(100)
BEGIN
DECLARE transtotal int;
DECLARE i int DEFAULT 0;
DECLARE total int;
CREATE TABLE IF NOT EXISTS `sim_lable`(
`label_id` int NOT NULL AUTO_INCREMENT PRIMARY KEY,
`doctype` varchar(10) NOT NULL,
`docid` int NOT NULL,
`doclineid` int NOT NULL,
`created` date NOT NULL,
`createdby` int NOT NULL
) ENGINE='InnoDB' COLLATE 'utf8_unicode_ci';
DELETE FROM sim_lable where
(TIME_TO_SEC(TIMEDIFF(paracreatedby,createdby))/60) >30;
SELECT #total=coalesce(count(*),0) as total2 from sim_label where
doctype=paradoctype and paradocid=docid;
IF total = 0 THEN
WHILE i < paraqty DO
INSERT INTO dbo.Students
(
`doctype` ,
`docid`,
`doclineid` ,
`created` ,
`createdby`
)
VALUES
(
paradoctype,
paradocid,
paradoclineid,
paracreated,
paracreatedby
) ;
END WHILE;
END IF;
END
As the error message says, explicit or implicit commit is not allowed in stored function or trigger. CREATE TABLE statement causes an implicit commit, therefore it is not allowed in a function.
Your code does not seem to return any value (there is no return statement in the function's body) and what you are doing there should be done in a stored procedure, rather than in a function.

Create function in MySQL

I'm just needing a little jump start, I'm a little confused how to do this. What I need to do is pull a 'customer_id' from the table and see how many items the customer has rented. If there are no 'customer_id' then it'll return 0. I'm just no quite grasping how to do this so any help is appreciated.
Create Table:
CREATE TABLE Customer
(name VARCHAR(30) NOT NULL,
address VARCHAR(70),
phone CHAR(10) NOT NULL,
customer_id INT(10) PRIMARY KEY NOT NULL);
Create Function: Have this partially started, but unsure if I'm doing it correctly.
DELIMITER $$
CREATE FUNCTION Num_Of_Rented(IN customer_id INT(10))
RETURNS INT(10)
BEGIN
DECLARE num INT(10);
SELECT IFNULL()
FROM
WHERE
RETURN num;
END $$
DELIMITER;
Inside your function, you need to select your value into your variable and then return your variable:
DECLARE num INT(10);
SELECT COUNT(field) INTO num
FROM table
WHERE condition;
RETURN num;
In your case:
DELIMITER $$
CREATE FUNCTION Num_Of_Rented(IN custId INT(10))
RETURNS INT(10)
BEGIN
DECLARE num INT(10);
SELECT COUNT(*) INTO num
FROM Customer C
WHERE C.customer_id = custId ;
RETURN num;
END $$
DELIMITER;

MySQL trigger after insert and after update

I have two tables with one named att as follows
CREATE TABLE att (
SID varchar(50) NOT NULL,
CID varchar(50) NOT NULL,
Date date NOT NULL,
H1 varchar(1) NOT NULL DEFAULT 0,
H2 varchar(1) NOT NULL DEFAULT 0,
H3 varchar(1) NOT NULL DEFAULT 0,
H4 varchar(1) NOT NULL DEFAULT 0,
H5 varchar(1) NOT NULL DEFAULT 0,
H6 varchar(1) NOT NULL DEFAULT 0,
H7 varchar(1) NOT NULL DEFAULT 0,
H8 varchar(1) NOT NULL DEFAULT 0,
H9 varchar(1) NOT NULL DEFAULT 0,
H10 varchar(1) NOT NULL DEFAULT 0,
INDEX (SID, CID)
);
The other table is per with following fields:
SID CID Per
How do I write a trigger for the following:
If an update occurs in any of the fields from h1-h0 on att table then
update the per column in the per table with the following values:
((total no of 1s - total no of 0s)/(total no of 1s + total no of 0s))/100
Thanks in advance
i Developed a Trigger,but it not working,its saying error in line 11,can you say what is the problem??
create TRIGGER `att_up` AFTER UPDATE ON `attentance`
FOR EACH ROW BEGIN
DECLARE Zeros INT;
DECLARE Ones INT;
DECLARE total INT;
DECLARE atted FLOAT;
SELECT SUM(8-(h1+h2+h3+h4+h5+h6+h7+h8))
INTO Zeros FROM attentance
WHERE StudID=NEW.StudID;
SELECT SUM(h1+h2+h3+h4+h5+h6+h7+h8)
INTO Ones FROM attentance
WHERE StudID=NEW.StudID;
SELECT SUM(8-(h1+h2+h3+h4+h5+h6+h7+h8))+ SUM(h1+h2+h3+h4+h5+h6+h7+h8)
INTO total FROM attentance
WHERE StudID=NEW.StudID;
set atted=((ZEROS-Ones)/total)/100;
INSERT into per(per) values (atted);
END$$
Make sure you change the delimiter before defining the trigger. Also make sure you're using the same table and column names when you create the table and the trigger (you're using att and attendance, and SID and StudID, in your examples).
As it is, the trigger definition caused no error when I tested it in MySQL 5.1.55 after setting the delimiter.
delimiter $$
CREATE TRIGGER `att_up`
AFTER UPDATE ON `attendance`
FOR EACH ROW
BEGIN
DECLARE Zeros INT;
DECLARE Ones INT;
DECLARE total INT;
DECLARE attend FLOAT;
SELECT SUM(8-(h1+h2+h3+h4+h5+h6+h7+h8)),
SUM(h1+h2+h3+h4+h5+h6+h7+h8),
SUM(8-(h1+h2+h3+h4+h5+h6+h7+h8)) + SUM(h1+h2+h3+h4+h5+h6+h7+h8)
INTO Zeros, Ones, Total FROM attendance
WHERE SID=NEW.SID;
SET attend=((Zeros-Ones)/total)/100;
INSERT INTO per (SID, CID, per) values (NEW.SID, NEW.CID, attend)
ON DUPLICATE KEY UPDATE per=attend;
END$$
delimiter ;
DELIMITER $$
CREATE TRIGGER `att_up` AFTER UPDATE ON `attentance`
FOR EACH ROW
BEGIN
SET #zeros = 0;
SET #ones = 0;
SET #total = 0;
SET #atted = 0;
(SELECT (8-SUM(h1)+SUM(h2)+SUM(h3)+SUM(h4)+SUM(h5)+SUM(h6)+SUM(h7)+SUM(h8)) INTO #zeros FROM attentance WHERE StudID=NEW.StudID);
(SELECT SUM(h1+h2+h3+h4+h5+h6+h7+h8) INTO #ones FROM attentance WHERE StudID=NEW.StudID);
(SELECT (SUM(8-(h1+h2+h3+h4+h5+h6+h7+h8))+ SUM(h1+h2+h3+h4+h5+h6+h7+h8)) INTO #total FROM attentance WHERE StudID=NEW.StudID);
SELECT (((#zeros-#ones)/#total)/100) INTO #atted FROM (SELECT 1) AS x;
END$$