簡體   English   中英

減少查詢數量

[英]Reduce the number of queries

我有單獨的查詢,但我需要減少“否”,因此將它們合而為一

 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

但這顯示了一些錯誤

使用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小提琴

如果要查找子集而不是filter_status的所有可能值,也可以將group_by與where子句一起使用:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM