如何在sql中联接3个表

问题描述 投票:0回答:1

如何将这三个表连接到所绘制的条件

enter image description here

select Name
from Artist
INNER JOIN Track ON Artist.aID = Track.aID
INNER JOIN CD ON CD.cID = Track.cID
where CD.Title = 'Compilation '

但是,为什么输出的是Artist.Table中的全名?

sql database join inner-join dbms-output
1个回答
0
投票

我不认为您的查询返回所有艺术家姓名;但是,您可能在结果集中多次重复使用相同的艺术家姓名。

为避免这种情况,您可以使用exists而不是联接:

select a.name 
from artist a
where exists ( 
    select 1
    from track t on 
    inner join cd c on c.cid = t.cid 
    where a.aid = t.aid and c.title = 'compilation'
)
© www.soinside.com 2019 - 2024. All rights reserved.