IF语句使用mariadb时出错

时间:2018-04-11 04:58:57

标签: mariadb

我已使用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

执行代码

2 个答案:

答案 0 :(得分:0)

我会回答我自己的问题。

根据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     |
+-------+

答案 1 :(得分:0)

Evem更简单:

SELECT IF(COUNT(*) = 0, NULL, COUNT(*))
    from additional_participants
    where username = 'JK1001';