我想使用以下查询从图库中获取前 5 行,但我只想从每行的 _gallery_gallery_items 中获取第一个匹配项。
例如,我想从画廊中获取狗画廊、猫画廊和后续画廊标题,但我只想从 _gallery_gallery_items 获取第一个匹配项,其中 _gallery_gallery_items.gallery_id = gallery.id。
所以结果应该是这样的:
id = 1
title = dog gallery
gallery_item_id = 70
id = 2
title = cat gallery
gallery_item_id = 102
id = 3
title = mouse gallery
gallery_item_id = 500
随后的 gallery_item_ids 不应出现在狗画廊中,例如。72, 72
到目前为止,这是我的查询:
SELECT gallery.id, gallery.title, _gallery_gallery_items.gallery_item_id
FROM gallery LEFT JOIN
_gallery_gallery_items ON gallery.id = _gallery_gallery_items.gallery_id
和表格:
画廊
id|title
1 |dog gallery
2 |cat gallery
3 |mouse gallery
_gallery_gallery_items
id|gallery_id|gallery_item_id
1| 1 | 70
2| 2 | 102
3| 1 | 71
4| 1 | 72
5| 3 | 500