来自加入 SQL 的多列中的单列
Single column from Multiple columns from join in SQL
我有一个这样的 table 输出,这是多个 table 连接的结果。
+---------+------+--------+------+
| Id | V1 | V2 | V3 |
+---------+------+--------+------+
| 1 | x1 | null | null|
| 2 | x2 | null | null|
| 3 | null | x3 | null|
| 4 | null | x4 | null|
| 5 | null | null | x9 |
+---------+------+--------+------+
我正在尝试获得这样的 table。
+---------+------+
| Id | V |
+---------+------+
| 1 | x1 |
| 2 | x2 |
| 3 | x3 |
| 4 | x4 |
| 5 | x5 |
+---------+------+
这就是我目前正在做的事情。不确定如何将三列合并为一列。
select a.identifier, a.v1, b.v2, c.v3,
from table a
full join table b on a.identifier = b.identifier
full join table c on a.identifier = c.identifier
where a.v REGEXP 'some condition'
看看 - COALESCE() Function,它 returns 列表中的第一个非空值。
如果你每行只有一个值——或者如果你只想要第一个——然后使用coalesce()
:
select id, coalesce(v1, v2, v3) as v
from t;
我有一个这样的 table 输出,这是多个 table 连接的结果。
+---------+------+--------+------+
| Id | V1 | V2 | V3 |
+---------+------+--------+------+
| 1 | x1 | null | null|
| 2 | x2 | null | null|
| 3 | null | x3 | null|
| 4 | null | x4 | null|
| 5 | null | null | x9 |
+---------+------+--------+------+
我正在尝试获得这样的 table。
+---------+------+
| Id | V |
+---------+------+
| 1 | x1 |
| 2 | x2 |
| 3 | x3 |
| 4 | x4 |
| 5 | x5 |
+---------+------+
这就是我目前正在做的事情。不确定如何将三列合并为一列。
select a.identifier, a.v1, b.v2, c.v3,
from table a
full join table b on a.identifier = b.identifier
full join table c on a.identifier = c.identifier
where a.v REGEXP 'some condition'
看看 - COALESCE() Function,它 returns 列表中的第一个非空值。
如果你每行只有一个值——或者如果你只想要第一个——然后使用coalesce()
:
select id, coalesce(v1, v2, v3) as v
from t;