mysql error on run query - mysql

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()));

Related

IF EXISTS Receiving Error

I am trying to run the following SQL cmd but am receiving ERROR 1064 (42000):
IF EXISTS(select 1 from namelist WHERE user='smitht' AND name='Tom Smith') BEGIN PRINT 'yes' END;
I've tested the select statement on its own and it works, so I'm not sure why I'm receiving this error.
Any ideas?
That is not how you use IF EXISTS in MYSQL. You may try this:
SELECT IF( EXISTS(
SELECT *
from namelist WHERE user='smitht' AND name='Tom Smith'))
According to the document you can't use IF statement like that, but you can use IF function:
SELECT IF (EXISTS(
select 1 from namelist WHERE user='smitht' AND name='Tom Smith'), 'yes', '')

Issue with stored procedure and COUNT IF statement

So I need to make a stored procedure called AddComment, that will add a comment to my Comments table , then add an entry into the Commenters table if that commenters name does not exist.
DELIMITER //
CREATE PROCEDURE AddComment(Name VARCHAR(60), Title VARCHAR(60), Comments VARCHAR(60))
BEGIN
INSERT INTO Comments(Name, Title, Comments)
VALUES (Name, Title, Comments);
DECLARE name_count INT;
SELECT COUNT(Name) INTO name_count
FROM Commenters
WHERE Name = Name;
IF name_count = 0
THEN INSERT INTO Commenters(Name)
VAlUES(Name);
ELSEIF name_count = 1
THEN INSERT IGNORE Commenters(Name)
VALUES(Name);
END IF;
END;
//
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version fo
r the right syntax to use near 'DECLARE name_count INT;
SELECT COUNT(Name) INTO name_count
FROM Commenters
^ Is the error I'm getting. I keep tweaking my code trying to figure it out, but nothing is working.
You need a cursor to use syntax like that, but you can use SET:
SET name_count = (SELECT COUNT(*) ...);
Or better yet, since you don't actually use the value, eliminate the variable entirely:
IF NOT EXISTS (SELECT * FROM ...) THEN

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)

mySQL If statement

I am trying to make this If Statement work, but I can't seem to make it do what I want. If I do a select #result, It'll give me the value 0, then why doesn't the IF statement work?
SET #message = '((sometihng here))';
select LEFT(#message, 1) into #firstChar;
select STRCMP(#firstChar,'(') into #result;
IF (#result = 0) THEN
SET #message = 'true';
//more selects and cals here;
END IF;
select #message;
I should get true, but I don't it shows me an error:
SQL query: IF( #result =0 ) THEN SET #message = 'true';
MySQL said:
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 (#result = 0) THEN SET #message = 'true'' at line 1
try use function http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html#function_if
SELECT IF(#result = 0, 'true', '((something here))') AS message
As Max Mara pointed out, that's a good work aroud. The reason the IF wasn't working is not because the syntax is incorrect, but because flow control functions like IF ... THEN are only valid inside of stored procedures or functions, All this thanks to #TehShrike
The IF .. THEN .. ELSE syntax in MySQL is only available for procedural code (stored precudures, functions, triggers..), but not for SELECT statements.
IF ELSE USED IN STORED PROCEDURE EXAMPLE BELOW
DELIMITER //
CREATE PROCEDURE NAME(IN Number INT)
BEGIN
IF roll= 1
THEN SELECT * FROM table1 WHERE id = roll;
ELSE
SELECT * FROM table2 WHERE id = 2;
END IF;
END //
DELIMITER ;

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