简体   繁体   English

在 MySQL 中连接两个表,从第二个表中只返回一行

[英]Join two tables in MySQL, returning just one row from the second table

I have two tables: gallery and pictures:我有两个表:画廊和图片:

gallery画廊

id           int (auto increment, primary key)
name         varchar

pictures图片

id           int (auto increment, primary key)
picture      varchar
gallery_id   int (foreign key)

How do I join these two tables showing for each row from the left table (gallery) just the first row from the second table, without going through all the rows from the second table?我如何加入这两个表,显示左表(图库)中的每一行,只显示第二个表的第一行,而不查看第二个表中的所有行? I am using MySQL.我正在使用 MySQL。

My objective is to make a page containing a list of the existing galleries showing a picture for each gallery as a link to the details page with all the pictures of that gallery.我的目标是制作一个包含现有画廊列表的页面,显示每个画廊的图片,作为指向包含该画廊所有图片的详细信息页面的链接。


I have searched this question on this site but the similar questions are too complicated.我在这个网站上搜索过这个问题,但类似的问题太复杂了。 I'm only interested in this simple example.我只对这个简单的例子感兴趣。

EDITED已编辑

Apparently grouping in MySQL database would do the trick for you.显然,在 MySQL 数据库中分组可以为您解决问题。

Database columns are main_id, sub_id, sub_main_id, sub_data数据库列有main_id, sub_id, sub_main_id, sub_data

SELECT *
FROM tblmain
  inner join sub on sub.sub_main_id = main_id
group by main_id;

without the group i have these records:没有组我有这些记录:

1, 1, 1, 'test 1'
1, 2, 1, 'test 2'
2, 3, 2, 'test 3'
3, 4, 3, 'test 4'
2, 5, 2, 'test 5'

after grouping, I get this result:分组后,我得到这个结果:

1, 1, 1, 'test 1'
2, 3, 2, 'test 3'
3, 4, 3, 'test 4'

Second option ( without grouping ) is by using internal row numbering, and limiting the row number to the first occurence.第二个选项(不分组)是使用内部行编号,并将行号限制为第一次出现。

set @gallery_id = '';
set @num  = 1;

SELECT * FROM gallery INNER JOIN ( select id, picture, gallery_id from ( select id, picture, gallery_id, @num := if(@gallery_id = gallery_id, @num + 1, 1) as row_number, @gallery_id := gallery_id as dummy from pictures ) as pictureRows where pictureRows.row_number = 1 ) as firstPicture ON firstPicture.gallery_id = gallery.id;

Hope this helps for you希望这对你有帮助

Solution 1 The method that I used is to add row number to the sub-set result (in our case pictures query) using ROW_NUMBER() function then in the join condition I added (rn = 1)... it would be something like the following:解决方案 1我使用的方法是使用ROW_NUMBER()函数将行号添加到子集结果(在我们的例子中为图片查询)中,然后在我添加的连接条件中(rn = 1)......它会像下列:

SELECT g.* FROM gallery g 
LEFT JOIN (
    SELECT *, ROW_NUMBER() OVER (ORDER BY id) rn FROM pictures
) p ON g.id = p.gallery_id AND p.rn = 1;

Edit: I didn't notice this is for MySQL my answer was for PostgreSQL, but I believe the technique is still valid if you know how to add row numbers to the query.编辑:我没有注意到这是针对 MySQL 我的答案是针对 PostgreSQL,但我相信如果您知道如何将行号添加到查询中,该技术仍然有效。

Solution 2: This is another technique you could use, without the need to add row_number of grouping, which is basically by adding sub-query in the join condition to pick only one row from related pictures (I know I'm sill using PostgreSQL, but I believe it would still be applicable in MySQL, or just overlook that and use the technique only):解决方案2:这是您可以使用的另一种技术,无需添加分组的row_number,这基本上是通过在连接条件中添加子查询以仅从相关图片中选择一行(我知道我还在使用PostgreSQL,但我相信它仍然适用于 MySQL,或者只是忽略它并仅使用该技术):

SELECT g.* FROM gallery g 
LEFT JOIN pictures p ON g.id = p.gallery_id AND p.id = (
    SELECT id FROM pictures WHERE gallery_id = g.id LIMIT 1
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 连接 MySQL 中的两个表,结果集中只有一行,从第二个表中选择了几列 - Join two tables in MySQL, have just one row in the result set with few columns selected from second table 如何连接两个 MySQL 表并检查第二个表中的行状态 - How to join two MySQL tables and check row status in second table MySQL内部连接两个表,并在匹配的行字段中将一个表的行插入另一个表 - MySQL Inner Join two tables and insert row from one table to other on matching row field MYSQL:查询两个表并将第二个表中的结果连接到一个数组 - MYSQL: Query two tables and join results from second table to an array 在mysql中联接两个表并从第二个表中读取值 - Join two tables in mysql and read values from the second table MYSQL连接两个表,第二个表具有匹配的值 - MYSQL join two tables, with matching value from second table MYSQL JOIN两个表限制第二个表的日期结果 - MYSQL JOIN two tables limit results from second table by date 当一个表丢失一行时,MySQL连接两个表 - MySQL join two tables when one table is missing a row MySQL INNER JOIN select 第二张表只有一行 - MySQL INNER JOIN select only one row from second table 使用Codeigniter将表中的一行与MySQL中其他两个表的多行联接 - Join one row from a table with multiple rows of two other tables in MySQL using Codeigniter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM