mysql-如何获取表的列名称为Null?
作者:互联网
考虑下表
--------------------------------
ID | ColA | ColB | ColC
--------------------------------
1 | ABC | |
2 | | XYZ |
3 | PQR | |
4 | MNO | PQR |
我需要获取ID = 1的表的第一个空闲列.
例如:
如果ID = 1,则下一个空闲列是ColB
如果ID = 2,则下一个空闲列为ColA
如果ID = 3,则下一个空闲列为ColB
如果ID = 4,则下一个空闲列是ColC
解决方法:
如果您想要列的名称,可以执行以下操作:
SQL> select id, cola, colb, colc,
2 coalesce(nvl2(cola, '', 'COLA'),
nvl2(colb, '', 'COLB'),
nvl2(colc, '', 'COLC')) first_free_col
3 from tab;
ID COL COL COL FIRST_FREE_COL
---------- --- --- --- --------------------------------
1 ABC COLB
2 XYZ COLA
3 PQR COLB
4 MNO PQR COLC
或案例
SQL> select id, cola, colb, colc,
2 case when cola is null then 'COLA'
3 when colb is null then 'COLB'
4 when colc is null then 'COLC'
5 end first_free_col
6 from tab;
ID COL COL COL FIRST_FREE_COL
---------- --- --- --- --------------------------------
1 ABC COLB
2 XYZ COLA
3 PQR COLB
4 MNO PQR COLC
标签:oracle,oracle10g,sql,mysql,sql-server 来源: https://codeday.me/bug/20191031/1974482.html