你好傢伙我需要在一個sql查詢中使用多個where子句,如下所示,但它不能工作,請幫助我。如何在一個SQL查詢中使用多個where子句
select (select count(total) as 'studentMarks1' from School where total <60),
(select count(total) as 'studentMarks2' from School where total >80)
from School
where Id = '8'
你好傢伙我需要在一個sql查詢中使用多個where子句,如下所示,但它不能工作,請幫助我。如何在一個SQL查詢中使用多個where子句
select (select count(total) as 'studentMarks1' from School where total <60),
(select count(total) as 'studentMarks2' from School where total >80)
from School
where Id = '8'
您最好使用CASE
語句像
select SUM(case when total < 60 then 1 else 0 end) as 'studentMarks1',
sum(case when total > 80 then 1 else 0 end) as 'studentMarks2'
from School
where Id = '8'
thnks @Rahul它的工作原理,我不知道他們爲什麼使用'SUM',所以如果我需要使用Sum作爲運算符'select sum(total)''那麼在查詢 – user3518835
@ user3518835不會有問題,這是有條件的總和,並且只有當條件滿足總和操作時纔會發生加1的總和,與執行計數操作相同。如果它必須是總和,那麼你可以像'選擇總和(情況當情況,然後總其他0結束)' – Rahul
哦!它炒鍋。對不起,我不熟悉'case',但它很棒,所以我想選擇所有類似於'select * where total> 50,where total = 40'這是如何工作的? – user3518835
您CAU通常用合適的CASE
語句做到這一點:
SELECT COUNT(CASE WHEN total < 60 then 1 else NULL END)
, COUNT(CASE WHEN total > 80 then 1 else NULL END)
FROM School
WHERE ID = '8'
的數據庫您使用的? – Bohemian