gpt4 book ai didi

mysql - 选择第一个加入的值,而不是全部

转载 作者:可可西里 更新时间:2023-11-01 08:48:15 24 4
gpt4 key购买 nike

我有表:

'gallery' - information about existing galleries 
id_gallery name date
========== ==== ===========
1 ... timestamp

'photo' - information and name of every photo in system
id_photo photo_name
======== ===========
1 some name

'photo_gallery' - "connecting" table, which says which photo are in which gallery
id_photo_gallery id_photo id_gallery
================ ================ =================
1 id from 'photo' id from 'gallery'

我需要从“照片”表中选择图库(带有一些关于它的信息,但这并不重要)并且只有一个照片 ID。

这(令人大吃一惊)返回所有照片。 (有关画廊的重复信息)

SELECT 
photo_gallery.id_photo as id_photo
FROM
gallery
JOIN
photo_gallery ON gallery.id_gallery = photo_gallery.id_gallery

编辑

这只返回一个画廊,不多。我需要为每个画廊检索一张照片 ...

SELECT 
photo_gallery.id_photo as id_photo
FROM
gallery
JOIN
photo_gallery ON gallery.id_gallery = photo_gallery.id_gallery LIMIT 1

最佳答案

请尝试以下查询:

SELECT galery.*, photo.* 
FROM galery
LEFT JOIN photo_gallery ON galery.id = photo_gallery.id_gallery
LEFT JOIN photo ON photo_gallery.id_photo = photo.id
GROUP BY galery.id

关于mysql - 选择第一个加入的值,而不是全部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21552758/

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