1

在我的数据库中,我有以下三个表,其中列出了字段:

TablePS 

 aID 

TableAM 

 aID(pk)
 bID  

TableM 

 bID(pk)
 bName 

如果我知道 TablePS 中的 aID,如何找到 TableM 中 bName 的值?我尝试了以下查询:

SELECT bName FROM TableM WHERE bID=
(SELECT TablePS.aID,TableAM.aID 
FROM TablePS 
INNER JOIN TableAM 
ON TablePS.aID= TableAM.aID)
4

4 回答 4

4
select bName
from tableM
    inner join tableAM on tableM.bID = tableAM.bID
where
    aID = [value]

应该足够了。如果你需要加入到表 PS,那么

select bName
from tableM
    inner join tableAM on tableM.bID = tableAM.bID
    inner join tablePS on tableAM.aID = tablePS.aID
于 2012-07-25T11:11:03.420 回答
1
Select BName from TableM as M inner join TableAM as Am on M.bId=Am.bID
inner join TablePS as P on Am.Aid=P.Aid
于 2012-07-25T11:11:55.697 回答
1
SELECT k.bName 
FROM TablePS p 
     INNER JOIN TableAM m ON m.aID = p.aID
     INNER JOIN TableM k on k.bID = m.bID
于 2012-07-25T11:12:24.623 回答
0

如果你不想使用JOINs 那么也许这个。

我假设 aId 是您可以更改的 INT 类型。

declare @x as int = 10 --<<change to an id that you are searching for

 select bname from  TableM  where bid in 
            (
            select  bID from TableAM  
            where aID in 
                             (
                             select aID from TablePS where aId = @x
                             ) 
            )
于 2012-07-25T12:32:00.683 回答