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

数据库数据库 2023-08-31 03:29:04 728
摘要: ...

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?的详细内容,更多请关注其它相关文章!