我想两次检索相同的列,但条件不同。
我的查询是这样的,但是以下查询检索了两个不同的列。但是我想两次获得同一列,如何实现呢?
Select name from tbCustomer where ID in (select ID from tbOldCustomer where oldID= 1)
Select name from tbCustomer where ID in (select ID from tbOldCustomer where oldID= 2)
tbCustomer 有两列:
ID 名称
1 aaa
2 bbb
3 ccc
4 ddd
tbOldCustomer 有两列:
ID 旧ID
1 2
2 1
3 1
4 2
4 1
想获取(1,2)中oldID的名称,输出应为以下内容:
名称 名称1
bbb aaa
ccc ddd
ddd
答案 0 :(得分:1)
使用存在
select t1.name
from tbCustomer t1
exists( select 1 from tbOldCustomer t2 where t1.id = t2.id
and t2.oldid in (1,2)
)
答案 1 :(得分:1)
我认为您可以仅使用JOIN
Select name
from tbCustomer tc
inner join tbOldCustomer toc On toc.id = tc.id
where toc.oldID IN (1,2)
答案 2 :(得分:0)
您可以尝试将conditional aggregation
与case when expression
一起使用
select max(case when oldID= 1 then name end) as name1,
max(case when oldID= 2 then name end) as name2
from tbCustomer join tbOldCustomer on ID=oldID where oldid in (1,2)
group by oldid
答案 3 :(得分:0)
您必须尝试
Select tc.name, toc.name from tbCustomer tc inner join
tbOldCustomer toc On toc.id = c.id where toc.oldID IN (1,2)
答案 4 :(得分:0)
请尝试这个。
Select name from tbCustomer where ID in (select ID from tbOldCustomer where oldID IN(1,2))
OR
Select A.name from tbCustomer A
INNER JOIN tbOldCustomer B
ON A.id = B.Id
AND B.OldId In (1,2)
答案 5 :(得分:0)
尝试一下
SELECT name
FROM tbCustomer tb1
JOIN (SELECT ID FROM tbOldCustomer WHERE oldID = 1 OR oldID= 2) tb2
ON tb1.ID = tb2.ID
WHERE tb1.ID IN (SELCT ID from tbOldCustomer where oldID in (1, 2))