我已经使用mariadb声明了IF
语句,如下所示。
set @number_of_technicians = (select COUNT(*) from additional_participants where username = 'JK1001');
IF @number_of_technicians > 0 THEN
select @number_of_technicians as amount;
END IF
当我执行代码时,它会抛出一些错误,如下所示。
[SQL]
set @number_of_technicians = (select COUNT(*) from additional_participants where username = 'JK1001');
Affected rows: 0
Time: 0.001ms
[SQL]
IF @number_of_technicians > 0 THEN
select @number_of_technicians as amount;
[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 2
我的代码有问题吗..?
我使用Navicat Premium
执行代码
发布于 2018-05-02 06:37:48
更简单:
SELECT IF(COUNT(*) = 0, NULL, COUNT(*))
from additional_participants
where username = 'JK1001';
发布于 2018-04-11 15:05:13
我会回答我自己的问题。
根据mariadb官方网站https://mariadb.com/kb/en/library/if-function/
的说法,这里是正确的IF
语句声明。
set @number_of_technicians = (select COUNT(*) from additional_participants where username = 'JK1001');
SELECT IF (@number_of_technicians > 0, (select @number_of_technicians as amount), (select 'null' as result) ) as result
上面的语句将给出如下结果。
+-------+
|result |
+-------+
| 1 |
+-------+
https://stackoverflow.com/questions/49766790
复制相似问题