现在有这么1个需求,小区有几个联系人,现在停电,先通知联系人1,如果没有则通知联系人2,否则还没有则通知联系人3.
create table test(
id number,
name varchar2(20),
contact_phone1 varchar2(20),
contact_phone2 varchar2(20),
contact_phone3 varchar2(20),
contact_phone4 varchar2(20)
);
insert into test values(1,'小区1','','','13485958522','');
insert into test values(1,'小区2','','','','13488958536');
commit;
SQL> select * from test;
ID NAME CONTACT_PHONE1 CONTACT_PHONE2 CONTACT_PHONE3 CONTACT_PHONE4
----- ------- -------------- --------------- -------------- --------------
1 小区1 13485958522
1 小区2 13488958536
如果没有这个函数,你可以要先把数据查出来以后,再用java代码处理。coalesce从左往右数,遇到第1个非null值,则返回该非null值,多层判断。
SQL> select id,name,coalesce(contact_phone1,
contact_phone2,
contact_phone3,
contact_phone4) as contact_phone
from test;
ID NAME CONTACT_PHONE
---------- -------------------- --------------------
1 小区1 13485958522
1 小区2 13488958536