MySQL Error querying from PHP: IF EXISTS - mysql

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

Related

Running the following query from my PhpMyadmin SQL tab

I'm running the following query from my PhpMyadmin SQL tab:
CREATE TRIGGER trg_bansach ON bill AFTER INSERT AS
BEGIN
UPDATE addbook
SET Quality = Quality - (
SELECT QualitySale
FROM inserted
WHERE book_id = addbook.book_id
)
FROM addbook
JOIN inserted ON addbook.book_id = inserted.book_id
END
But everytime I'm getting this error msg:
#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 'FROM addbook
WHERE addbook.book_id = inserted.book_id
END' at line 10
Use delimiter
DELIMITER //
CREATE TRIGGER trg_bansach ON bill AFTER INSERT AS
BEGIN
UPDATE addbook
SET Quality = Quality - (
SELECT QualitySale
FROM inserted
WHERE book_id = addbook.book_id
)
FROM addbook
JOIN inserted ON addbook.book_id = inserted.book_id;
END
//
DELIMITER ;

Creating a trigger which enters value in studies(rollno,code) table only if total credits of a student are less than 30

I am getting the 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 '' at line 5.
CREATE TRIGGER db BEFORE INSERT ON studies
FOR EACH ROW
BEGIN
IF((select CAST(SUM(credits) as UNSIGNED) from ((SELECT credits from subject WHERE subject.code = NEW.code) union all (SELECT credits from subject,(SELECT code from studies where studies.rollno = NEW.rollno) as t1 WHERE subject.code = t1.code)) as t2) > 30) THEN
set NEW.rollno = NULL;
ENDIF;
END

mysql error on run query

This is my query in mysql (I use phpmyadmin to execute the query).
but I can't execute....
can help me please?
SELECT count(*) into #cnt FROM `oie_option` WHERE `opt_name` =CONCAT('earning1391',pmonth('2015-09-09')))
if(#cnt <=0)then
INSERT INTO oie_option ('opt_name','opt_value') VALUES (CONCAT('earning1391',pmonth('2015-09-09')),1000);
end if;
And this is the 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 ')
if(#cnt <=0)then
insert into oie_option ('opt_name','opt_value') values(CON' at line 1
Why?
Thanks
I think you are missing a semicolon in first query.
SELECT count(*) into #cnt FROM `oie_option`
WHERE `opt_name` =CONCAT('earning1391',pmonth('2015-09-09'));
if(#cnt <=0)then
INSERT INTO oie_option ('opt_name','opt_value') VALUES
(CONCAT('earning1391',pmonth('2015-09-09')),1000);
end if;
I tryed to reproduce your situation and this is what I got:
You need to put your code inside of a strored procedure or function to use the IF statement https://stackoverflow.com/a/12954385/5308054.
Here you can see a feature request to fix it https://bugs.mysql.com/bug.php?id=48777
You could avoid using the if statement with query like this
INSERT INTO oie_option ('opt_name','opt_value')
SELECT CONCAT('earning1391',pmonth('2015-09-09')),1000
FROM oie_option
WHERE (SELECT count(*) FROM `oie_option` WHERE `opt_name`=CONCAT('earning1391',pmonth('2015-09-09')))=0
LIMIT 1;
I declared the variable and corrected.
DECLARE cnt INT DEFAULT 0;
SELECT count(*) INTO cnt FROM `oie_option` WHERE `opt_name` = CONCAT( 'earning1391', pmonth (now()));

SQL If Statment / select into in a stored procedure

I cannot understand why the following SQL procedure will not store on my database and is reporting an error, I've been at it for ages and am totally puzzled.
DELIMITER $$
CREATE PROCEDURE add_brand(IN inBrandName TEXT)
BEGIN
DECLARE brandexists INT DEFAULT 0;
SELECT count(*) WHERE BrandName = inBrandName INTO brandexists;
IF brandexists = 1 THEN
select "exists";
ELSE
INSERT INTO tblBrand (BrandName) VALUES (inBrandName);
SELECT LAST_INSERT_ID();
END IF;
END
The error i'm getting is this:
#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 'WHERE BrandName = inBrandName INTO brandexists; IF brandexists = 1 THEN sele' at line 6
any ideas?
You are missing a table where you select from:
SELECT count(*) WHERE BrandName = inBrandName INTO brandexists;
^---here (from some_table)

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.