0

我有一堆包含一堆艺术品的 CD。我想在网页上显示每张 CD 的缩略图图像,为此我需要从该 CD 存在的许多艺术品中只选择一个图像...

该查询选择所有艺术品,我按位置分组并希望选择第一个。在我订购它们之后,我不知道如何为每条记录挑选第一张图片......

查询如下:

SELECT release_artwork.release_id, release_artwork.position, artwork.small_filename, artwork.small_width, artwork.small_height
FROM release_artwork
JOIN artwork ON artwork.artwork_id = release_artwork.artwork_id
ORDER BY release_artwork.position

在此处输入图像描述

4

3 回答 3

4

我没有方便的 MySQL 实例,但这应该可以 - 只需定义一个内联表来保存最小值,然后加入它。

SELECT release_artwork.release_id, release_artwork.position, artwork.small_filename, artwork.small_width, artwork.small_height
FROM
  release_artwork
  inner JOIN artwork ON artwork.artwork_id = release_artwork.artwork_id
  inner join
  (
  SELECT
    ra.release_id as release_id
   ,min(ra.position) as rap
  FROM
    release_artwork ra
  group by
    ra.release_id
  ) mins
  on mins.release_id = release_artwork.release_id
    and mins.rap = release_artwork.position
ORDER BY
  release_artwork.release_id
于 2012-12-28T19:17:46.147 回答
2

在这种情况下,您可以使用LIMIT 1, 将检索到的结果限制为第一个。

于 2012-12-28T19:06:30.763 回答
0
SELECT release_artwork.release_id, release_artwork.position, artwork.small_filename, artwork.small_width, artwork.small_height
FROM release_artwork
JOIN artwork ON artwork.artwork_id = release_artwork.artwork_id
ORDER BY release_artwork.position LIMIT 1

在末尾添加LIMIT 1会将其限制为单个结果。

于 2012-12-28T19:08:12.433 回答