日期:2014-05-17  浏览次数:20708 次

求一条Sql语句
有表A
id     nId1     nId2
---------------
1       1           2
(nId1,nId2对就表B中的ID)

表B
id   Name
-------
1     AAA
2     BBB

要求查询出如
id     Name1   Name2
---------------
1       AAA       BBB

Sql语句应该怎么写
除了
select   A.id,B1.Name,B2.Name  
from   A,B   B1,B   B2  
where   A.nId1   =   B1.id   and   A.nId2   =   B2.id   and   A.id   =   1
外,还能怎么实现!

------解决方案--------------------
这样也是可以的:
select a.id,
(select name from B where b.nId1 = a.id) as Name1,
(select name from B where b.nId2 = a.id) as Name2
from A a
------解决方案--------------------
select a.id as id ,(select name from b where b.id=a.nid1) as name1,(select name from b where b.id=a.nid2) as name2 from a