Can I use IF in an SQL sentence in MySQL? - mysql

UPDATE `order` SET `status` = IF `type` = 2 THEN 1 ELSE 2 END IF;
I wrote this sentence in a MySQL trigger, and I got this error prompt:
Error Code: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'type = 2 THEN 1 ELSE 2 END IF at line 5
How can I fix this error?

You're looking for CASE:
UPDATE `order`
SET `status` = CASE WHEN `type` = 2
THEN 1
ELSE 2
END;

You need to use IF() function not IF statement:
UPDATE `order`
SET `status` = IF(`type` = 2, 1, 2);

Related

triggers in mysql getting an error

I'm trying to create a trigger that will update some value, and if the row is not exists than insert a new row with value = 1.
I wrote the trigger in 3 possible ways and all of them are getting error
version 1:
CREATE TRIGGER stat_trg BEFORE INSERT ON comments
FOR EACH ROW
BEGIN
INSERT INTO statistics
SET item_id = NEW.item_id,
likes = 0,
comment = 1
ON DUPLICATE KEY UPDATE
SET comment = OLD.comment + 1;
END;
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
near 'SET comment = OLD.comment + 1' at line 9
version 2:
CREATE TRIGGER stat_trg AFTER INSERT ON comments
FOR EACH ROW
BEGIN
DECLARE num integer DEFAULT 0;
SELECT comment into num FROM statistics
WHERE item_id = OLD.item_id;
SET num := num + 1;
INSERT INTO statistics (item_id, comment, likes) VALUES (num,1,0)
END;
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '' at line 4
version 3:
CREATE TRIGGER stat_trg BEFORE INSERT ON comments
FOR EACH ROW
BEGIN
INSERT INTO statistics (item_id, likes, COMMENT) VALUES (NEW.item_id,0,1)
ON DUPLICATE KEY UPDATE
comment = comment + 1;
END;
1064 - You have an error in your SQL syntax; check the manual that corresponds > to your MySQL server version for the right syntax to use near '' at line 6
All looks good according to examples that I found, what am I missing?
Please help on this issue.
Thanks.
Please try this:
DELIMITER $$
CREATE TRIGGER `stat_trg` BEFORE INSERT ON `comments`
FOR EACH ROW BEGIN
INSERT INTO `statistics` set item_id= NEW.item_id, likes = '0', comments = '1' ON DUPLICATE KEY UPDATE
comments = values(comments) + 1;
END$$
DELIMITER ;

Mysql Trigger with IF and IF ELSE conditions

i have table 'comments' with fields 'type','receiver_id' ,'confirm' and 'id'.
there are three types of object that each have comment section.
after comment confirm the trigger increase the number of objects in their respective table.
DB::unprepared('CREATE TRIGGER comment_confirm
AFTER UPDATE ON comments FOR EACH ROW
BEGIN
IF OLD.confirm = 0 AND NEW.confirm = 1 THEN
IF OLD.type = profile THEN
UPDATE profiles SET comments = comments + 1 WHERE user_id = OLD.reciever_id;
ELSE IF OLD.type = blog THEN
UPDATE blogs SET comments = comments + 1 WHERE user_id = OLD.reciever_id;
ELSE IF OLD.type = topic THEN
UPDATE topics SET comments = comments + 1 WHERE user_id = OLD.reciever_id;
END IF;
END IF;
END
');
the error for migration is:
[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax
to use near '' at line 13
Else if should be elseif (no space) IF OLD.type = profile doesn't look right should be in single quotes for a string comparison.

Error 1064:You have an error in your SQL syntax

I want run this query but get an error:
Error 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UPDATE `ads` SET `aDesc` = replace(aDesc, 'amp;', '')' at line 3
My query is:
UPDATE `ads`
SET `aName` = replace(aName, 'amp;', '')
UPDATE `ads`
SET `aDesc` = replace(aDesc, 'amp;', '');
What's the problem?
Your query looks like two queries without a separating delimiter.
The more efficient option is to do both changes in one query:
UPDATE ads
SET aName = replace(aName, 'amp;', ''),
aDesc = replace(aDesc, 'amp;', '');
but if you must run two queries:
UPDATE ads SET aName = replace(aName, 'amp;', '');
UPDATE ads SET aDesc = replace(aDesc, 'amp;', '');

Conditional update/insert in MySQL

I found here a topic about an MySQL IF, ELSE query,i adapted it but i can't figure it out what is the problem with it.
Here is the query:
IF (SELECT * FROM `jos_import03_07_2011` WHERE `cod_oem` = 'OP-4CL') IS NULL THEN
INSERT INTO `jos_import03_07_2011` (`tip_imp`, `tip_produs`, `producator`,
`cod_intern`, `desc`, `cod_oem`, `pret`, `valuta`) VALUES ('Imprimanta Laser',
'Piese Schimb', 'BROTHER', 'BR-200503', '', 'OP-4CL', '338.49', 'EUR');
ELSE UPDATE `jos_import03_07_2011` SET `pret` = '338.49' WHERE `cod_oem` = 'OP-4CL';
END IF;
And here is the error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL
server version for the right syntax to use near 'IF (SELECT * FROM
`jos_import03_07_2011` WHERE `cod_oem` = 'OP-4CL') IS NULL THE' at line 1
This is the original post:
Conditional mySQL statement. If true UPDATE, if false INSERT
Thanks,
Sebastian
UPDATE
Error code for IF EXISTS:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL
server version for the right syntax to use near 'IF EXISTS (SELECT * FROM
`jos_import03_07_2011` WHERE `cod_oem` = 'OP-4CL') THEN' at line 1
Any reason you can't use the INSERT ... ON DUPLICATE KEY syntax?
INSERT INTO `jos_import03_07_2011` (`tip_imp`, `tip_produs`, `producator`,
`cod_intern`, `desc`, `cod_oem`, `pret`, `valuta`)
VALUES ('Imprimanta Laser', Piese Schimb', 'BROTHER', 'BR-200503', '', 'OP-4CL', '338.49', 'EUR')
ON DUPLICATE KEY UPDATE SET pret = VALUES(pret)
would be far more efficient: one less query and far less code to debug.

Error in my stored-procedure

i have an error in my stored-procedure. I use MySql DB
SET #counter = 1;
SET #last = 0;
UPDATE Customer SET ordre = (IF(#last = customer_id,#counter + 1,#counter = 1)),
#last = customer_id
My Error
Script line: 3 You have an error in
your SQL syntax; check the manual that
corresponds to your MySQL server
version for the right syntax to use
near '#last = customer_id ORDER BY
customer_id' at line 2
You cannot set variables in SET clause of UPDATE statement. '#last = customer_id' causes the error.
From the reference -
UPDATE syntax - '...SET col_name1=expr1 [, col_name2=expr2 ...]'
The SET clause indicates which columns to modify and the values they should be given.