[英]Joins - get row from one table then get all rows that match in another table? but first table matching row should not be repeated
I have two tables.我有两张桌子。 Gallery and Joins.画廊和加入。
Gallery
id | title
1 | Dog Gallery
2 | Cat Gallery
Joins
id | gallery_id | picture_id
1 | 1 | 100
2 | 1 | 101
3 | 2 | 56
4 | 1 | 102
I want to get id, gallery title from gallery - where id is equal to a specific id but also get all of the rows from joins where gallery id equals a specific gallery id.我想从画廊中获取 id、画廊标题 - 其中 id 等于特定的 id,但也从画廊 id 等于特定画廊 id 的连接中获取所有行。
So for above if the id was 1. I would want the dog gallery from gallery and picture_ids 100,101 and 102 from joins.所以对于上面如果 id 是 1。我想要来自画廊的狗画廊和来自连接的 picture_ids 100,101 和 102。
now the problem is that gallery title should comes one time.现在的问题是画廊标题应该出现一次。 result is title=dog gallery picture_id=100,title=dog gallery picture_id=101,title=dog gallery picture_id=102 i need the result like this title=dog gallery picture_id=100,picture_id=101 and picture_id=102结果是title=dog gallery picture_id=100,title=dog gallery picture_id=101,title=dog gallery picture_id=102 我需要这样的结果title=dog gallery picture_id=100,picture_id=101 和picture_id=102
You can use GROUP_CONCAT to have all the IDs as string您可以使用GROUP_CONCAT将所有 ID 作为字符串
SELECT G.*, GROUP_CONCAT(DISTINCT P.picture_id ORDER BY P.picture_id DESC SEPARATOR ',')
FROM gallery as G
LEFT JOIN pictures as P ON P.gallery_id = G.id
GROUP BY G.id;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.