mysql 运行 查询错误

mysql error on run query

这是我在 mysql 中的查询(我使用 phpmyadmin 来执行查询)。 但我无法执行.... 可以帮我吗?

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;

这是错误:

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

为什么? 谢谢

我认为您在第一个查询中缺少分号。

    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;

我试图重现你的情况,这就是我得到的结果:

  1. 您需要将代码放在存储过程或函数中才能使用 IF 语句 。 在这里您可以看到修复它的功能请求 https://bugs.mysql.com/bug.php?id=48777
  2. 你可以避免像这样在查询中使用 if 语句

    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;
    

感谢亲爱的朋友们 我声明变量并更正。

> 声明 cnt INT 默认值 0; SELECT count(*) INTO cnt FROM oie_option WHERE opt_name = CONCAT( 'earning1391', pmonth (now()));