mysql查询

uhry853o  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(198)

我有两张这样的table:

Table 1: tracks
id  | artist     | track
---------------------------------------
1     Tom Smith    This Time is Right Time
2     Tom Smith    Oh Yes
3     John Doe     Every Time I See You

Table 2: festival_bands
id  | fest_title | fest_artist
---------------------------------------
1     Hoe Down Fest 2019    John Doe
2     Copperland Fest       Tom Smith
3     Copperland Fest       Reggie Wisk
4     Copperland Fest       Tom Smith
5     Copperland Fest       John Doe
6     Bluegrass Memories    John Doe

对于表2中的每个节日列表,我只需要显示表1中的一个“曲目”,如下所示:结果:

Copperland Festival:
-----------------------
Tom Smith    This Time is Right Time
John Doe     Every Time I See You

用外行的话说,逻辑是这样的:从表1中只得到一个曲目,其中艺术家等于(或匹配)表2中的fest\u艺术家
我引用了一个类似的问题,该问题向以下方向提出了建议: $sql="select * from tracks WHERE (artist) in (select fest_artist from festival_bands group by name)" 但是运气不好。

erhoui1w

erhoui1w1#

可以使用相关子查询为艺术家获取随机轨迹:

select fb.*,
       (select t.track
        from tracks t
        where t.artist = fb.fest_artist
        order by t.counter desc
       ) as most_popular_track
from festival_bands fb;

相关问题