Conditional update/insert in MySQL - 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.

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 ;

Can I use IF in an SQL sentence in 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);

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;', '');

select if and update issue

Am using this sql to do an update on duplicate key
IF (SELECT COUNT(*) FROM `mlm_settings` WHERE `key` = 'notify_type' AND `user_id`=7 >0 )
UPDATE mlm_settings SET value='2' WHERE user_id = 7
ELSE
BEGIN
INSERT INTO `mlm_settings` (`key`, `value`,`user_id`) VALUES ('notify_type', '2',7)
END
by i get an sql error in mysql saying
#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 'IF (SELECT COUNT(*) FROM
`mlm_settings` WHERE `key` = 'notify_type' AND `user_id' at line 1
i can't figure what is the cause of the error, as the names of the tables are valid and the values are of the same data type
what could be the error?
You can only use IF statements in stored procedures, not normal queries.
If there's a unique index on (key, user_id) in the table, you can use:
INSERT INTO mlm_settings (`key`, value, user_id) VALUES ('notify_type', '2', 7)
ON DUPLICATE KEY UPDATE value = '2';
IF control block cannot be used OUTSIDE of functions. Try this:-
SELECT IF( EXISTS(
SELECT COUNT(*) FROM `mlm_settings` WHERE `key` = 'notify_type' AND `user_id`=7 >0), 1, 0)

MySQL Error querying from PHP: IF EXISTS

I have this PHP Code with Query to mysql database:
$query2 = "IF( EXISTS (SELECT * FROM shipcargo WHERE shipid='$shipid' AND item='$item' AND price='$price'))
BEGIN
UPDATE shipcargo SET amount = amount+'$amount' WHERE shipid='$shipid' AND item='$item' AND price='$price'
END
ELSE
BEGIN
INSERT INTO shipcargo (shipid, item, amount, price) VALUES('$shipid', '$item', '$amount', '$price')
END";
mysql_query($query2) or die(mysql_error());
The Error Returned is:
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 shipcargo WHERE shipid='11' AND item='WheatBastard' AN' at line 1
syntax is wrong! Take a look at http://dev.mysql.com/doc/refman/5.0/en/exists-and-not-exists-subqueries.html
I believe it's
IF EXISTS (SELECT * FROM
Rather than
IF (EXISTS ( SELECT
You don't need a bracket with the IF statement