我的SQL查询在'== 1
附近返回语法错误我的查询是
SELECT COUNT( IF ( application_type== 1 ) ) as total from table_1
答案 0 :(得分:5)
用例何时
SELECT COUNT(case when application_type=1 then 1 end) as total from table_1
OR
SELECT COUNT(*) as total from table_1
where application_type=1
OR
SELECT count(if(application_type=1, 1, NULL)) as total from table_1
答案 1 :(得分:2)
语法有多个问题。这有效:
SELECT COUNT(*) as total from table_1 WHERE application_type = 1;
答案 2 :(得分:0)
您的条件IF(application_type == 1)实际上返回TRUE或FALSE,即1或0。现在,如果对包含1或0的列应用COUNT,它将始终返回表的总行数。因此,您需要调整查询。无论如何,我认为使用SUM代替COUNT会达到您的目的-
SELECT SUM(IF( application_type = 1)) AS total from table_1
答案 3 :(得分:0)
完全不使用if()
或case
。 MySQL方便地将布尔值视为整数,因此您只需执行以下操作即可:
SELECT SUM( application_type = 1 ) as total
FROM table_1;
当然,如果您只想这样做,请使用COUNT(*)
并将条件移至WHERE
子句。