我有三个名为“Public”,“Private”和“Normal”的表。表的优先级是私有高于正常和公共。每个表可能有也可能没有重复数据。我的问题是 如果所有表都有相同的数据,它应该从高优先级表返回,而不是从所有表返回。 例如
PRIVATE TABLE
==============================
id | Mobileno
==============================
1 | 9999999999
------------------------------
2 | 8888888888
------------------------------
NORMAL TABLE
==============================
id | Mobileno
==============================
1 | 9999999988
------------------------------
2 | 8888888888
------------------------------
PUBLIC TABLE
==============================
id | Mobileno
==============================
1 | 9999999977
------------------------------
2 | 8888888888
------------------------------
我想要像
这样的结果==============================
Mobileno | type
==============================
9999999999 | private
------------------------------
8888888888 | private
------------------------------
9999999988 | normal
------------------------------
9999999977 | public
答案 0 :(得分:1)
尝试此查询:
select t1.Mobileno,
case when t1.Mobileno in (select Mobileno from PRIVATE) then 'PRIVATE'
when t1.Mobileno not in (select Mobileno from PRIVATE)
and t1.Mobileno in (select Mobileno from NORMAL) then 'NORMAL'
else 'PUBLIC' end as type
from
(select * from PRIVATE
union
select * from NORMAL
union
select * from PUBLIC)t1;