MySQL CASE语句在什么情况下返回NULL?

2023年 8月 27日 67.1k 0

MySQL CASE语句在什么情况下返回NULL?

mysql> Select CASE 100
-> WHEN 150 THEN 'It is matched'
-> WHEN 200 THEN 'It is not matched'
-> END As 'It Returns NULL';
+-----------------+
| It Returns NULL |
+-----------------+
| NULL |
+-----------------+
1 row in set (0.00 sec)

下面的查询使用表“Students”中的数据,返回 NULL,因为没有学生属于 WI 国家/地区。

mysql> Select SUM(CASE WHEN country = 'USA' THEN 1 ELSE 0 END) AS USA,
-> SUM(CASE WHEN country = 'UK' THEN 1 ELSE 0 END) AS UK,
-> SUM(CASE WHEN country = 'INDIA' THEN 1 ELSE 0 END) AS INDIA,
-> SUM(CASE WHEN country = 'Russia' THEN 1 ELSE 0 END) AS Russia,
-> SUM(CASE WHEN country = 'France' THEN 1 ELSE 0 END) AS France,
-> SUM(CASE WHEN country = 'NZ' THEN 1 ELSE 0 END) AS NZ,
-> SUM(CASE WHEN country = 'Australia' THEN 1 ELSE 0 END) AS Australia,
-> SUM(CASE WHEN country = 'WI' THEN 1 END) AS WI
-> From Students;
+------+------+-------+--------+--------+------+-----------+------+
| USA | UK | INDIA | Russia | France | NZ | Australia | WI |
+------+------+-------+--------+--------+------+-----------+------+
| 2 | 1 | 2 | 1 | 1 | 1 | 1 | NULL |
+------+------+-------+--------+--------+------+-----------+------+
1 row in set (0.00 sec)

登录后复制

以上就是MySQL CASE语句在什么情况下返回NULL?的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

Oracle如何使用授予和撤销权限的语法和示例
Awesome Project: 探索 MatrixOrigin 云原生分布式数据库
下载丨66页PDF,云和恩墨技术通讯(2024年7月刊)
社区版oceanbase安装
Oracle 导出CSV工具-sqluldr2
ETL数据集成丨快速将MySQL数据迁移至Doris数据库

发布评论