不言不语

您现在的位置是: 首页 >  数据库  >  MYSQL

MYSQL

MySQL中case when语句的用法

2022-06-03MYSQL
mysql数据库中case when语句,用于计算条件列表并返回多个可能结果表达式之一。代码如下:


select subject,sum(case when score<60 then 1 else 0 end) as NO,sum(case when score>=60 then 1 else 0 end) YES from score group by subject;


通上以上MySQL语句,查询出科目及格人数和不及格人数:

+---------+------+------+
| subject | NO   | YES  |
+---------+------+------+
| linux   |    0 |    2 |
| mysql   |    0 |    2 |
| php     |    1 |    1 |
+---------+------+------+


文章评论