gpt4 book ai didi

MySQL 4 表连接

转载 作者:行者123 更新时间:2023-11-29 12:09:07 24 4
gpt4 key购买 nike

我一直在尝试为这个 4 表连接获取正确的数据,但我似乎无法确定它。我正在尝试从 songs 获取 id 列表基于我在 follows 中关注的人,如果我的关注者是creator,则显示歌曲ID轨道或只是转发它。

SELECT DISTINCT songs.id
FROM songs
LEFT JOIN follows ON follows.follower = 6
LEFT JOIN tracks ON tracks.creator = follows.following
LEFT JOIN reshares ON reshares.resharer = follows.following
WHERE
songs.id = tracks.containing_song
OR
songs.id = reshares.song
ORDER BY songs.last_updated DESC LIMIT 1

我不太确定出了什么问题,但我对数据库连接的知识非常有限。

最佳答案

您可能需要的是两个集合的并集:

  • 以下是 id=6 的用户创建的歌曲

  • 关注 id=6 的用户转发的歌曲

由于您尚未提供表架构,因此我将使用您在查询中提供的名称。

( SELECT DISTINCT s.id, s.last_updated
FROM follows f
JOIN tracks t ON t.creator = f.following
JOIN songs s ON t.containing_song = s.id
WHERE f.follower = 6 )
UNION
( SELECT DISTINCT s.id, s.last_updated
FROM follows f
JOIN reshares r ON r.resharer = f.following
JOIN songs s ON r.song = s.id
WHERE f.follower = 6 )
ORDER BY last_updated DESC

关于MySQL 4 表连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30992919/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com