想要同時統計男生數量和不及格數量。
SELECT COUNT(1) AS boyNum FROM t_student WHERE sex='男';
SELECT COUNT(1) AS poorNum FROM t_student WHERE score<'60';
失敗的嘗試:
SELECT COUNT(sex='男') AS boyNum, COUNT(score<'60') AS poorNum FROM t_student;
解決方法:
mysql提供if函數,可以在查詢是使用。
SELECT
SUM(
IF((sex='男'),1,0)
) ‘boyNum’,
SUM(
IF((score<'60'),1,0)
) ‘poorNum’
FROM t_student;