Mysql以相反的顺序选择列,忽略空值

时间:2015-09-08 20:21:18

标签: mysql

我有这样的数据集: 我想忽略空值,以相反的顺序选择这些列。

col1 col2 col3 col4
1     2    3    null
1     4   null  null
1    3     5    null
1    null  null null

我想在此选择它:

col1 col2 col3 col4
3    2    1    null
4   1    null  null
5   3    1     null
1   null null  null

使用大量列有一个很好的方法吗?

1 个答案:

答案 0 :(得分:0)

您可以使用COALESCE()功能,并以相反的顺序选择具有不同列别名或名称的列。

select coalesce(col3, 0) as col1,
coalesce(col2, 0) as col2,
coalesce(col1, 0) as col3,
col4
from your_table;