0

我有一个艺术家的身份,我想知道这个艺术家发行的所有专辑。

MySQL 结构

MySQL结构

示例 Mysql 查询

SELECT album_name 
FROM albums AS a, 
     tracks AS t, 
     produced AS p 
WHERE p.produced_track = t.track_id 
  AND t.track_album = a.album_id 
AND p.produced_artist = "17"

问题

由于一张专辑有很多曲目,我的结果会显示同一张专辑的几次。为了制作唱片,我想拥有一位艺术家的所有专辑。有没有办法在 MySQL 中做到这一点?

4

3 回答 3

2

只需 SELECT DISTINCT用作:

SELECT DISTINCT album_name 
FROM albums AS a, 
 tracks AS t, 
 produced AS p 
WHERE p.produced_track = t.track_id 
AND t.track_album = a.album_id 
AND p.produced_artist = "17"

这个应该做...

于 2013-05-12T16:19:58.783 回答
2

除了检索太多行并删除重复项之外,您还可以使用子查询

SELECT album_name 
FROM albums AS a
WHERE album_id IN (SELECT t.track_album
                   FROM tracks t
                      JOIN produced AS p ON p.produced_track = t.track_id 
                   WHERE p.produced_artist = 17)

您还应该习惯使用显式JOIN语法而不是隐藏在WHERE子句中的隐式连接

于 2013-05-12T16:23:34.210 回答
1

你应该试试: SELECT DISTINCT album_name FROM albums AS a, tracks AS t, produced AS p WHERE p.produced_track = t.track_id AND t.track_album = a.album_id AND p.produced_artist = "17"

于 2013-05-12T16:20:06.863 回答