简体   繁体   English

连接 - 从一个表中获取行然后获取另一个表中匹配的所有行? 但不应重复第一个表匹配行

[英]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.

相关问题 如何从表中获取所有数据,如果在同一列中重复相同的值,则该行应计数一次? - How to get get all the data from table and if same value repeated in one column that row should count once? 读取一个表并从另一个表中获取与第一个表行相关的行 - Read one table and get rows from another table relevant to first tables row 一个父行,另一个表中的多个子行。 如何将它们全部排成一排? - One parent row, multiple child rows in another table. How to get them all in one row? 我想将一个表中的行与另一张表中的行进行匹配,并且每行只应匹配一次 - I want to match rows in one table with rows in another table and each row should just be matched once 如何从两个表(如一个表的所有行)以及相对于其他表的第一个表的主键总和中获取数据 - How to get data from two tables like one table all rows and with respect to first table primary key sum of row from other table Laravel Eloquent select 来自一个表,如果另一个表有匹配则加入第一行 - Laravel Eloquent select from one table and join first row if there's a match from another table 从一个表中选择所有行,并为另一表中的每一行选择一个特定值 - Select all rows from one table and one specific value for each row from another table mysql-根据ID和DATE从一个表中获取一行,如果不显示空值,则在另一表中获取多行 - mysql - taking one row from one table based on ID and DATE and get multiple rows in another table if not show null values 将表中的许多行插入另一表中的唯一行 - Insert many rows from a table into one unique row in another table 从一个表中选择结果,并为每一行从另一张表中选择具有相同ID的所有行 - Select results from one table and for each row select all rows that have the same id from another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM