gpt4 book ai didi

MySQL 从两个表与一个左连接

转载 作者:行者123 更新时间:2023-11-29 18:27:25 24 4
gpt4 key购买 nike

我有两个表,它们的列几乎相同 - 视频列表和画廊列表。然后我想将其左连接到成员表,这样我就可以从成员 ID 中获取成员姓名。我最终得到的是按日期列排序的视频和画廊列表。我知道如何单独完成,但无法使其组合起来工作。以下是单独的查询:

//Video Query
SELECT videos.vid, videos.mid, videos.source, videos.category, videos.title, videos.description, videos.date_added, videos.status, members.name FROM videos LEFT JOIN members ON videos.mid=members.mid WHERE videos.status = 'published' ORDER BY date_added

//Gallery Query
SELECT galleries.vid, galleries.mid, galleries.directory, galleries.category, galleries.title, galleries.description, galleries.date_added, galleries.status, members.name FROM videos LEFT JOIN members ON videos.mid=members.mid WHERE galleries.status = 'published' ORDER BY date_added

但我无法让它与两个表的左连接一起使用。两个表都使用同一列进行左连接,并使用同一列进行排序。我可以执行一个查询还是必须查询两者并进行某种合并?

最终结果将类似于:

- Video A by John
- Gallery A by John
- Video B by John
- Video C by Mary

等等

谢谢!

最佳答案

您可以组合查询并编写类似的内容

SELECT 
videos.vid,
videos.mid,
videos.source,
videos.category,
videos.title,
videos.description,
videos.date_added,
videos.status,
galleries.vid,
galleries.mid,
galleries.directory,
galleries.category,
galleries.title,
galleries.description,
galleries.date_added,
galleries.status,
members.name
FROM
videos
LEFT JOIN members
ON videos.mid = members.mid members.name
FROM
videos
LEFT JOIN members
ON videos.mid = members.mid
AND videos.status = 'published'
LEFT JOIN galleries
ON galleries.mid = members.mid
AND galleries.status = 'published'
ORDER BY galleries.date_added,videos.date_added

我希望这会有所帮助。

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

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