一个表中有很多字段,可以为空,怎么得到第一个非空字段。
FIELD1 FIELD2 FIELD3 FIELD4 FIELD5
A 1 C 1 D1
B2 C2
C3
每个字段都可能为空 取得 一条记录中第一个非空的字段。
[解决办法]
- SQL code
create table tb(col1 int,col2 int,col3 int)insert into tbselect null,null,1 union allselect null,2,1goselect coalesce(col1,col2,col3) colfrom tbdrop table tb/************************col-----------12(2 行受影响)