例如,有一个主表:
+----+---------+
| Id | column1 |
+----+---------+
| 1 | a |
| 2 | b |
| 3 | c |
+----+---------+
第二个加入的表:
+----+---------+
| Id | column2 |
+----+---------+
| 1 | Value |
| 2 | NULL |
+----+---------+
column2包含字符串值,但此处的NULL值为NULL类型,而不是“NULL”字符串。
所以在加入两个表后我想要实现的是这样一个表:
+----+---------+-------------+
| Id | column1 | column2 |
+----+---------+-------------+
| 1 | a | Value |
| 2 | b | Null Value |
| 3 | c | Not Exist |
+----+---------+-------------+
是否有可能通过MySQL来区分列中的NULL值和不存在的列?
答案 0 :(得分:4)
SELECT
m.Id, m.column1,
CASE WHEN s.Id IS NULL
THEN 'Not Exists'
WHEN s.column2 IS NULL
THEN 'Null Value'
ELSE s.column2
END AS column2
FROM
mainTable AS m
LEFT JOIN
secondTable As s
ON s.Id = m.Id ;
中进行测试