我想检索同一列两次,但条件不同。
我的查询是这样的,但是下面的查询检索两个不同的列。但是我想要两次得到同一个专栏,我怎么能做到这一点呢?
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 name
1 aaa
2 血脑屏障
3 ccc
4 ddd
tbOldCustomer有两列:
ID oldID
1个 2
2个 1
3个 1
4个 2
4个 1
希望获得oldID在(1,2)中的名称,并且输出应该是如下所示:
name name1
血bbb aaa
ccc ddd
ddd
发布于 2019-03-18 10:05:14
使用存在
select t1.name
from tbCustomer t1
exists( select 1 from tbOldCustomer t2 where t1.id = t2.id
and t2.oldid in (1,2)
)发布于 2019-03-18 10:06:57
我认为您可以通过使用简单的JOIN来实现这一点。
Select name
from tbCustomer tc
inner join tbOldCustomer toc On toc.id = tc.id
where toc.oldID IN (1,2)发布于 2019-03-18 10:06:13
您可以尝试将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 oldidhttps://stackoverflow.com/questions/55218812
复制相似问题