我们应该如何消除 NULL 值而不是将其保留在单独的行中,值必须驻留在列中,因此通过使用执行结果集中的联合集运算符使 NULL 值消失?
考虑简单的 SQL 查询
SELECT
*
FROM
(
(SELECT
6 + 2 AS addition,
NULL AS subtraction,
NULL AS multiplication,
NULL AS division
FROM
DUAL)
UNION
(SELECT
NULL AS addition,
6 - 2 AS subtraction,
NULL AS multiplication,
NULL AS division
FROM
DUAL)
UNION
(SELECT
NULL AS addition,
NULL AS subtraction,
6 * 2 AS multiplication,
NULL AS division
FROM
DUAL)
UNION
(SELECT
NULL AS addition,
NULL AS subtraction,
NULL AS multiplication,
6 / 2 AS division
FROM
DUAL)
) A;
实际结果:
+----------+-------------+----------------+----------+
| addition | subtraction | multiplication | division |
+----------+-------------+----------------+----------+
| 8 | NULL | NULL | NULL |
| NULL | 4 | NULL | NULL |
| NULL | NULL | 12 | NULL |
| NULL | NULL | NULL | 3.0000 |
+----------+-------------+----------------+----------+
4 rows in set (0.00 sec)
预期结果:
+----------+-------------+----------------+----------+
| addition | subtraction | multiplication | division |
+----------+-------------+----------------+----------+
| 8 | 4 | 12 | 3.0000 |
+----------+-------------+----------------+----------+
1 row in set (0.00 sec)