MySQL IF THEN无法使用多个IF THEN ELSEIF

时间:2019-06-10 10:04:02

标签: mysql

我有这个查询:

SET @foundMarker:=0;
SELECT id, channel
FROM myTable
WHERE (
   IF (id like 'm%' AND id = 'm2') THEN SET @foundMarker=1;
       ELSEIF (id like 'm%' and @foundMarker=1) THEN SET @foundMarker=0;
       END IF;
   )
ORDER BY LOCATE(CONCAT('"', id, '"'), sort);

所以我需要在这里完成的操作是检查是否找到了'm2'标记,将foundMarker标志设置为1并读取下一行。...再次检查,如果我发现一个以'm'开头的标记(例如'm4')将foundMarker标志设置为0。

所以我以这种方式显示标记之间的值(其他值是整数类型)。

所以我陷入了错误:

IF (id like 'm%' AND id = 'm2') THEN SET @foundMarker=1;
ELSEIF (id like 'm%' and @foundMarker=1) THEN SET @foundMarker=0;
ELSEIF (@foundMarker=1) THEN @showChannels:=(id not like 'm%' and @showChannels)
END IF;

如何正确编写它以便查询将运行并设置用户定义的变量?我阅读了MySQL手册页,但是上面的代码无法运行查询...我得到了错误:

ERROR 1064 (42000): 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 ') THEN SET @foundMarker=1' at line 29

ERROR 1064 (42000): 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 'ELSEIF (id like 'm%' and @foundMarker=1) THEN SET @foundMarker=0' at line 1

ERROR 1064 (42000): 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 'END IF' at line 1

如果可以使用简单,快速的查询来编写以上IF,请告诉我。

0 个答案:

没有答案