查看数据库表中的数据

时间:2014-04-15 08:37:06

标签: sql database view

SELECT dun, COUNT( id_ahli ) AS JUMLAH_KESELURUHAN, COUNT(kaum='melayu') AS melayu, COUNT(kaum='cina') AS cina 
FROM maklumat_ahli
WHERE jantina = 'lelaki'
AND 
(kematian_tarikh IS NULL)
AND (bayaran_pertama IS NULL)
AND (bayaran_kedua IS NULL)
GROUP BY dun
ORDER BY dun

这是我的sql语句。是否可以通过kaum计算和查看数据?我使用那个sql语句,但我的计数不正确

/-----------------------------------------/
|dun | Jumlah_keseluruhan | melayu | cina |
-------------------------------------------
|A   |123                 |100     |23    |
-------------------------------------------

是否可以像上表一样从db查看数据。

1 个答案:

答案 0 :(得分:2)

要按特殊值计算数据,您可以使用CASE子句

COUNT(case when kaum='melayu' then 1 else 0 end) AS melayu, 
COUNT(case when kaum='cina'the 1 else 0 end) AS cina