简体   繁体   English

显示所有相册的专辑封面

[英]Display album cover for all photo gallery albums

I have a photo gallery where I allow the user to create individual photo albums. 我有一个相册,允许用户创建个人相册。 I can display the name of each album like this. 我可以这样显示每个专辑的名称。

$album = DB::getInstance()->query("SELECT `album_id`,`album_date`,`album_title` FROM `albums` ORDER BY `album_date` DESC");

What I'm trying to do is add a cover photo for each album, but can't seem to get my query correct. 我想做的是为每张专辑添加一张封面照片,但似乎无法使我的查询正确。 This is what I'm trying. 这就是我正在尝试的。

$album = DB::getInstance()->query("SELECT
albums.album_id,
albums.created,
albums.album_date,
albums.album_title,
gallery.file_name,
gallery.id,
gallery.album_id
FROM
albums
INNER JOIN gallery ON gallery.album_id = albums.album_id ORDER BY gallery.id DESC ;");

This is the result I get: 这是我得到的结果:

在此处输入图片说明

This is the result I'm trying to achieve: 这是我想要达到的结果:

在此处输入图片说明

I want each album name displayed with the last photo from each album as the cover photo. 我希望每个专辑名称与每个专辑的最后一张照片一起作为封面照片显示。

If I add a LIMIT of 1 to my query, only one album is shown. 如果我在查询中添加LIMIT为1,则只会显示一张专辑。

Here are my tables that are Joined together by a Foreign Key. 这是我的通过外键连接在一起的表。

Albums Table: 专辑表:

在此处输入图片说明

Gallery Table: 图库表:

在此处输入图片说明

Any solution to get this working would be much appreciated. 任何解决方案,以使这项工作将不胜感激。

You can select the most recent image in each album using the following query: 您可以使用以下查询选择每个相册中的最新图像:

SELECT album_id, MAX(id) id
FROM gallery
GROUP BY album_id

Alternatively, you can select a random image in each album like this: 或者,您可以像这样在每个相册中选择一个随机图像:

SELECT g.album_id, (
    SELECT id FROM gallery g2
    WHERE g2.album_id = g.album_id
    ORDER BY RAND()
    LIMIT 1
) id
FROM gallery g
GROUP BY g.album_id

Either of the above queries could be plugged into a complete query: 上述查询中的任何一个都可以插入完整的查询中:

SELECT a.album_id, a.created, a.album_date, a.album_title,
       g.file_name, g.id, g.album_id
FROM albums a
INNER JOIN (
    # above query goes here
) alb_img USING (album_id)
INNER JOIN gallery g ON g.id = alb_img.id
ORDER BY a.album_date DESC;

If you'd like to select empty albums as well as those containing one or more images change the INNER JOINs to LEFT JOINs. 如果您想选择空白相册以及包含一张或多张图像的相册,请将INNER JOINs更改为LEFT JOINs。

Give this a try: 试试看:

SELECT album_id, album_name, title, COALESCE(file_name, 'mydefaultimage.jpeg') 
  FROM albums
  LEFT JOIN gallery on album_id = album 
  ORDER BY added DESC
  LIMIT 1,1

if there's no image, title will be null and filename will contain the default image name. 如果没有图像,则标题将为null ,文件名将包含默认图像名称。

[EDIT] no , after the DESC [编辑],DESC

Try this: 尝试这个:

SELECT
    `album`.`album_title`
    , (SELECT file_name FROM gallery WHERE gallery.`album_id` = album.`album_id` ORDER BY file_name DESC LIMIT 1) AS file_name
FROM
    `album`

The first thing to do is to determine the latest id for the image for each album: 首先要做的是确定每个专辑的图像的最新ID:

SELECT album_id, MAX(id) id FROM gallery GROUP BY album_id

You can easily put this as inline view (subquery) in a query 您可以轻松地将其作为内联视图(子查询)放入查询中

SELECT
    a.album_name, g.file_name, g.title
FROM
    (SELECT album_id, MAX(id) id FROM gallery GROUP BY album_id) a
JOIN
    gallery g USING (album_id, id)
JOIN
    albums a USING (album_id)

Solution: 解:

SELECT albums.album_id,
       albums.album_title,
       albums.album_date,
       albums.created,
       gallery.id,
       gallery.file_name
FROM `albums`
LEFT JOIN `gallery` ON (albums.album_id = gallery.album_id)
WHERE gallery.id IN
    ( SELECT MAX(gallery.id)
     FROM `gallery`
     GROUP BY gallery.album_id)

Explanation: 说明:

You first need to select the latest record corresponding to each album from gallery table. 您首先需要从gallery表中选择与每个专辑相对应的最新记录。 Group the records with album_id field, and user the aggregate function MAX() to obtain the record with biggest id in the grouped result. 将记录与album_id字段album_id ,并使用聚合函数MAX()获得分组结果中ID最大的记录。

SELECT MAX(gallery.id)
     FROM `gallery`
     GROUP BY gallery.album_id

then join the gallery table with albums table with LEFT JOIN and use the results of the above query in WHERE clause. 然后使用LEFT JOINgallery表与albums表连接起来,并在WHERE子句中使用上述查询的结果。 You can specify the whole set of fields you need to use in the SELECT part of the outer query 您可以在外部查询的SELECT部分中指定需要使用的整个字段集

SELECT albums.album_id,
       albums.album_title,
       albums.album_date,
       albums.created,
       gallery.id,
       gallery.file_name
FROM `albums`
LEFT JOIN `gallery` ON (albums.album_id = gallery.album_id)
WHERE gallery.id IN
    ( FIRSTQUERY....)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM