减少查询次数

时间:2015-10-17 05:14:59

标签: mysql codeigniter

我有单独的查询,但我需要减少no以便将所有内容放在一个

 select   count(applicant_id)  as registered from student_application where filter_status=0 AND
  select  count(applicant_id) as filer_select  from student_application where filter_status=1 AND 
   select  count(applicant_id) as filter_reject  from student_application where filter_status=2

但这显示了一些错误

2 个答案:

答案 0 :(得分:3)

使用CASE表达式。

<强>查询

select 
count(case when filter_status = 0 then applicant_id else null end) as registered,
count(case when filter_status = 1 then applicant_id else null end) as filer_select,
count(case when filter_status = 2 then applicant_id else null end) as filer_reject
from student_application;

SQL Fiddle

答案 1 :(得分:2)

如果你正在寻找一个子集而不是filter_status的所有可能值,你也可以使用group_by和where子句:

SELECT filter_status, COUNT(*)
FROM student_application 
WHERE filter_status in (0, 1, 2) 
GROUP BY filter_status;