开发者

MySQL find count query

I have table called stats. In am inserting yes or no in 开发者_如何学Gothe table, and I want to show the number of yes count and the number of no count.

Can somebody please help me with the query?


select yn, count(*)
from stats
group by yn;


Try something like this

SELECT  SUM(CASE WHEN recommend = 'Y' THEN 1 ELSE 0 END) YesCount,
        SUM(CASE WHEN recommend = 'N' THEN 1 ELSE 0 END) NoCount,
        COUNT(*) TotalCount
FROM    Stats


This is exactly what the GROUP BY clause and aggregate functions are for in SQL. The following should be what you need and more efficient then a CASE statement. It returns a table with two columns: recommend and no (which is the count of identical values in the recommend column. If what you said above is true, then this should return at most two rows.

SELECT recommend, count(*) AS no FROM stats GROUP BY recommend
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜