-3
count(case when gender='M' then 1 end) as male_cnt
任何人都可以解釋我們爲什麼使用then 1
?count(性別='M'則爲1的情況下)作爲male_cnt可以解釋我們爲什麼使用1
count(case when gender='M' then 1 end) as male_cnt
任何人都可以解釋我們爲什麼使用then 1
?count(性別='M'則爲1的情況下)作爲male_cnt可以解釋我們爲什麼使用1
你的陳述包括記錄中有多少人。並且count()
計算不是null
的一切。所以,你可以在case
count(case when gender='M' then 'hello' end)
使用比NULL
其他任何沒有else
部分在case
這使得自動null
。因此,這是一樣的:
count(case when gender='M' then 'hello' else null end)
因爲這一切都是相當混亂,我寧願使用sum()
代替:
sum(case when gender='M' then 1 else 0 end)
這並不相同,但國際海事組織是更具可讀性。
在MySQL中,你可以減少到
sum(gender = 'M')
因爲MySQL中的比較的結果是0
或1
,你可以總結這個結果了。