Where can I use a single select statement to implement MySQL query statistics? It is too useful. For example, if you want to query the number of people who pass or fail, how can you query it?
The simple statement for MySQL query statistics must be as follows:
- select a.name,count_neg,count_plus from
- (select count(id) as count_plus,name from score2 where score >=60 group by name) a,
- (select count(id) as count_neg,name from score2 where score <=60 group by name) b
- where a.name=b.name
-
That is, at least two statements must be used.
Today, we just found that mysql supports if, so we can use if to implement it creatively:
- select name, sum(if(score>=60,1,0)),sum(if(score<60,1,0)) from score2 group by name
-
It is easy to use a single select statement to implement MySQL query statistics.
If the principle is greater than 60, the value is 1, and sum is the count.
MySQL query results are sorted by a certain value
Non-empty question in MySQL Query
How to adjust the MySQL Query Buffer
How MySQL query optimizer works
In-depth parsing of MySQL query Cache Mechanism