sql语句中if语句。
先看一下数据库中的数据:
mysql> select * from student order by class ,score;
+----+-------+-------+-------+
| id | name | class | score |
+----+-------+-------+-------+
| 2 | user | 1 | 45 |
| 4 | user | 1 | 62 |
| 3 | user1 | 1 | 80 |
| 1 | user1 | 1 | 100 |
| 5 | user1 | 1 | 100 |
| 8 | user | 2 | 24 |
| 7 | user1 | 2 | 90 |
| 6 | user | 2 | 108 |
+----+-------+-------+-------+
8 rows in set (0.00 sec)
if语句结果:
mysql> SELECT class,sum(IF(score>60,1,0)) JIGE ,sum(IF(score<60,1,0)) BUJIGE FROM student GROUP BY class ;
输出结果:
+-------+------+--------+
| class | JIGE | BUJIGE |
+-------+------+--------+
| 1 | 4 | 1 |
| 2 | 2 | 1 |
+-------+------+--------+
2 rows in set (0.00 sec)
打完收工!
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/4487.html