1

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

Micaela
  • 132
  • 13

3 Answers3

0

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;
Tarun Upadhyay
  • 724
  • 2
  • 7
  • 16
0

I tryed to reproduce your situation and this is what I got:

  1. 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
  2. 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;
    
Community
  • 1
  • 1
Dzmitry Paliakou
  • 1,587
  • 19
  • 27
0

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()));
General Grievance
  • 4,555
  • 31
  • 31
  • 45