小编典典

内连3桌

sql

我正在使用PHP和PDO,我需要重新识别连接3个表的信息:

  • photos
  • albums
  • album_photos

该表具有以下结构:

相片:

photo_id  (int)
path      (varchar)
nick      (varchar)
date      (timestamp)

专辑

album_id   (int)
album_name (varchar)
nick       (varchar)
date       (timestamp)

album_photos

album_id (int)
photo_id (int)
nick     (varchar)
date     (timestamp)

因此,我想显示所有相册,每张相册最多包含5张照片, 用户昵称是“所有者”

如下所示:

album_name_1:
[photo_1]
[photo_2]
[photo_3]
[photo_4]
[photo_5]

album_name_2:
[photo_1]
[photo_2]
[photo_3]
[photo_4]
[photo_5]

我只知道可以使用INNER JOIN来制作类似的东西,但是我找不到如何用3个桌子来制作它。

有例子或其他帮助我可以做到这一点吗?


阅读 150

收藏
2021-04-28

共1个答案

小编典典

SELECT a., c.date
FROM Album a
INNER JOIN Album_Photo b
ON a.Album_ID = b.Album_ID
INNER JOIN Photo c
ON b.Photo_ID = c.Photo_ID
WHERE c.Nick = ‘owner’ AND
(
SELECT COUNT(
)
FROM album_photo d
WHERE b.album_id = d.album_id AND
d.nick = ‘owner’ AND
b.date >= d.date
) <= 2 // <<== change this value to 5

2021-04-28