查询一个表中所有id字段在另一个表中对应值的SQL语句怎么写?

2024-12-23 13:52:20
推荐回答(5个)
回答1:

子查询即可
SELECT (SELECT name FROM TA WHERE id = TB.id1) AS id1,
(SELECT name FROM TA WHERE id = TB.id2) AS id2
FROM TB

回答2:

select c.name,d.name from (select a.id1 id1,a.id2 id2,a.name name from TA a,TB b where a.id1=b.id1) temp c
,TA d where c.id2=d.id2

回答3:

SELECT T1.name as name1,T2.name as name2 FROM TA as T1,TA as T2,TB
WHERE T1.id = TB.id1 AND T2.id = TB.id2

回答4:

--创建测试数据
create table #a(id int,name varchar(10))
insert into #a values(1,'A')
insert into #a values(2,'B')
insert into #a values(3,'C')

create table #b(id1 int,id2 int)
insert into #b values(1,2)
insert into #b values(2,1)
insert into #b values(2,3)

---------------实现代码部分----------------
select
t1.name as name1
,t2.name as name2
from
#b tt
left join #a t1 on tt.id1 =t1.id
left join #a t2 on tt.id2 =t2.id

回答5:

select * from ta,tb where ta.id= tb.id1