选择only not null列值,并使用sql查询仅显示该列值
我有这样的桌子选择only not null列值,并使用sql查询仅显示该列值,sql,Sql,我有这样的桌子 col1 col2 col3 col4 ---- ---- ---- ---- 1 NULL NULL NULL NULL 2 NULL NULL 3 NULL NULL NULL NULL NULL NULL 4 预期结果是: col5 ---- 1 2 3 4 如何使用SQL查询获取此类输出?您正在查找coalesce(),如果需要第一个非空值: select t.*, coalesce(
col1 col2 col3 col4
---- ---- ---- ----
1 NULL NULL NULL
NULL 2 NULL NULL
3 NULL NULL NULL
NULL NULL NULL 4
预期结果是:
col5
----
1
2
3
4
如何使用SQL查询获取此类输出?您正在查找
coalesce()
,如果需要第一个非空值:
select t.*, coalesce(col1, col2, col3, col4) as col5
from t;
试试这个
SELECT
(CASE
WHEN col1 IS NOT NULL THEN col1
WHEN col2 IS NOT NULL THEN col2
WHEN col3 IS NOT NULL THEN col3
WHEN col4 IS NOT NULL THEN col4
END) AS col5 FROM table
你的头衔表明:
select t.*
from (select col1 as col5
from table union all
select col2
from table union all
. . .
) t
where col5 is not null;
在这种方法中,我们只能比较列,然后结果将始终为真,因此我只比较其中不能为空值的字段。您使用的是哪种RBDM?SQL Server,Oracle,…?您正在使用哪个数据库???查找COALESCE()
Select * from your_table
WHERE col1 and col2 and col3 and col4 and col5 IS NOT NULL;