从sql联接的多列中选择单列

lzfw57am  于 2021-08-13  发布在  Java
关注(0)|答案(2)|浏览(333)

我有一个这样的表输出,它是多个表连接的结果。

+---------+------+--------+------+
|   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'
dced5bon

dced5bon1#

如果每行只有一个值,或者只需要第一个值,那么使用 coalesce() :

select id, coalesce(v1, v2, v3) as v
from t;
mzaanser

mzaanser2#

如果有一个-coalesce()函数,它将返回列表中的第一个非空值。

相关问题