在我的数据库中,我有以下三个表,其中列出了字段:
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)
答案 0 :(得分: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
答案 1 :(得分: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
答案 2 :(得分: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
答案 3 :(得分:0)
如果您不想使用JOIN
,那么可能就是这样。
我假设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
)
)