lee
lee

Reputation: 17

Select statement with count

I want to count the number of ABC group using id.

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

What's wrong? Many thanks.

Upvotes: 0

Views: 57

Answers (3)

Battle Mage
Battle Mage

Reputation: 379

If you want to get COUNT of users, who has the "group" field = "ABC"

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

Also, it's better to avoid using SQL keywords in column names (GROUP is an SQL keyword)

Upvotes: 0

Harshil Doshi
Harshil Doshi

Reputation: 3592

Try this:

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

Upvotes: 0

Unnikrishnan R
Unnikrishnan R

Reputation: 5031

Include the columns in the select list in group by clause when using aggregate functions.

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

Else simply get the count with out using other columns in the select statement.

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

Upvotes: 1

Related Questions