Skip to content
Advertisement

Given a list, how to get the count from mysql table?

Given a string list, I want to get the count of each item from the table. However, I don’t know how to get the count(0) of item which does not exist in the table.

for example, I have a table as follow.

Given a list [“aaa”, “bbb”, “ccc”] , I hope a query can return me a result like

I tried “select name, count(*) from table where name in (“aaa”, “bbb”, “ccc”) group by name;”, but the result is without the count of “bbb”. Can I do it in a single query?

Advertisement

Answer

Your values list must be a rowsource, not a criteria in WHERE.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement