2016-10-17 139 views
0

我想使用id來計算ABC組的數量。Select count with count

SELECT group, count(id) as total FROM `user` WHERE group=`ABC`; 

怎麼了? 非常感謝。

+0

我會問你同樣的,怎麼回事? –

+3

你使用了什麼[標籤:rdbms]?你有什麼錯誤? – Mureinik

+0

這是無效的SQL - 你需要在'group by'語句中包含''group''列。 –

回答

1

使用聚合函數時,在group by子句的選擇列表中包含列。

SELECT group, count(id) as total FROM user 
WHERE group=`ABC` 
GROUP BY group 

否則只需使用select語句中的其他列進行計數即可。

SELECT count(id) as total FROM user 
WHERE group=`ABC` 
0

試試這個:如果你想獲得用戶的COUNT,誰擁有 「組」 域= 「ABC」

SELECT count(id) as total FROM user WHERE group='ABC'; 

而且

SELECT group, count(id) as total FROM `user` 
    group by group having group like 'ABC'; 
0

,最好避免在列名中使用SQL關鍵字(GROUP是一個SQL關鍵字)