我有两张桌子 表1:
id name posId Mid 1 sam 1 10 2 sid 1 10 3 jeet 1 10
表2:
id name posid Mid 1 Anin 2 10 2 Nir 2 10 3 jeev 2 10
我想要一张像......这样的桌子。
posid 1 2
即;我想要有明显的" posid"通过加入table1和table2,其中" Mid" table1和table2将是相同的
答案 0 :(得分:1)
您可以这样做:
select distinct t1.posId
from t1
where not exists (select 1 from t2 where t2.posId = t1.posId)
union all
select distinct t2.posId
from t2
where not exists (select 1 from t1 where t2.posId = t1.posId);
我认为我错误地解决了这个问题。您可以使用join
:
select t1.posid, t2.posid
from t1 join
t2
on t1.mid = t2.mid;
要将此作为列,您需要取消忽略。这是一种方法:
select distinct (case when n.which = 1 then t1.posid else t2.posid end) as posid
from t1 join
t2
on t1.mid = t2.mid cross join
(select 1 as which union all select 2 as which) n
答案 1 :(得分:0)
我知道你希望两个表都有明显的posid
。
这不高效但有效:
select distinct aa.posid
from
(select distinct table1.posid as posid
from table1
join table2
on table1.mid=table2.mid
UNION ALL
select distinct table2.posid as posid
from table1
join table2
on table1.mid=table2.mid) aa
答案 2 :(得分:0)
您可以将join
与union
select t1.posid
from t1 join t2 on t1.mid = t2.mid
union
select t2.posid
from t1 join t2 on t1.mid = t2.mid