Saturday, September 10, 2011

mysql count if

http://forums.mysql.com/read.php?10,203876,204010#msg-204010
select book_name,     count(if(rating=1, 1, null)) as "POOR",     count(if(rating=2,1,null)) as "Average",     count(if(rating=3,1,null)) as "Good",     count(if(rating=4,1,null)) as "Great" from rating group by book_name order by book_name; +----------------+------+---------+------+-------+ | book_name      | POOR | Average | Good | Great | +----------------+------+---------+------+-------+ | Javascript     |    0 |       1 |    0 |     0 |  | SQL Cookbook   |    0 |       1 |    1 |     1 |  | Visual Basic 6 |    1 |       0 |    1 |     0 |  +----------------+------+---------+------+-------+

IF(expression, return this if expression is true, return this if expression is false)

No comments:

Related Posts Plugin for WordPress, Blogger...