MySQL – How to Count Rows in Table Based on Condition

MySQL

I have table, table B:

id   p.id    s.id
1      5       1
2      5       1 
3      5       2
4      5       2
5      5       3

The result looks like this:

s.id   count
1       2
2       2
3       1

I have tried:

Select count(s.id) from B group by s.id 

But its not giving me the required result. I am using MySQL.

Best Answer

It's simple:

SELECT s.id
     , count(*) as COUNT
FROM B
GROUP BY s.id;