在SQL中使用不同的条件获取同一列两次

在SQL中使用不同的条件获取同一列两次,sql,conditional-statements,Sql,Conditional Statements,我想检索相同的列两次,但条件不同 我的查询是这样的,但下面的查询检索两个不同的列。但是我想得到相同的专栏两次,我如何才能做到这一点 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有

我想检索相同的列两次,但条件不同

我的查询是这样的,但下面的查询检索两个不同的列。但是我想得到相同的专栏两次,我如何才能做到这一点

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

希望获取oldID位于(1,2)中的名称,并且输出应为以下内容:

名称名称1
bbb  aaa
ccc  ddd
ddd

使用存在

select t1.name 
from tbCustomer t1 
exists( select 1 from  tbOldCustomer t2  where  t1.id = t2.id
                                       and t2.oldid in (1,2)
        )
使用存在

select t1.name 
from tbCustomer t1 
exists( select 1 from  tbOldCustomer t2  where  t1.id = t2.id
                                       and t2.oldid in (1,2)
        )

当表达式

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

当表达式

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

我认为您只需使用
JOIN

Select name 
from tbCustomer tc 
inner join tbOldCustomer toc On toc.id = tc.id
where toc.oldID IN (1,2)

我认为您只需使用
JOIN

Select name 
from tbCustomer tc 
inner join tbOldCustomer toc On toc.id = tc.id
where toc.oldID IN (1,2)
你必须试一试

Select tc.name, toc.name from tbCustomer tc inner join 
tbOldCustomer toc On toc.id = c.id where toc.oldID IN (1,2)
你必须试一试

Select tc.name, toc.name from tbCustomer tc inner join 
tbOldCustomer toc On toc.id = c.id where toc.oldID IN (1,2)
请试试这个

Select name from tbCustomer where ID in (select ID from tbOldCustomer where oldID IN(1,2))

请试试这个

Select name from tbCustomer where ID in (select ID from tbOldCustomer where oldID IN(1,2))

试试这个

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))
试试这个

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))


向我们展示一些示例表数据和预期结果。都是格式化文本,而不是图像。@jarlh:附加图像。你一定是在开玩笑吧。。。我清楚地写了“作为格式化文本,而不是图像”,而您仍然附加了一个图像。好笑。查询中的列名与图像中的列名不匹配。@jarlh:updated!向我们展示一些示例表数据和预期结果。都是格式化文本,而不是图像。@jarlh:附加图像。你一定是在开玩笑吧。。。我清楚地写了“作为格式化文本,而不是图像”,而您仍然附加了一个图像。好笑。查询中的列名与图像中的列名不匹配。@jarlh:updated!您的查询只检索一列。但我想用不同的值重复“name”-coulmn两次。@darma:你的问题完全不清楚。您需要将示例数据和预期结果添加为jarlhsuggested@Susang:更新!您的查询只检索一列。但我想用不同的值重复“name”-coulmn两次。@darma:你的问题完全不清楚。您需要将示例数据和预期结果添加为jarlhsuggested@Susang:更新!这里的问题是:tbOldCustomer中没有“名称”列。所以在toc.name给我一个错误这里的问题是:tbOldCustomer中没有“name”列。所以在toc.name给我一个错误