【问题标题】:selecting id and count(id) from table in mysql从 mysql 中的表中选择 id 和 count(id)
【发布时间】:2023-03-19 10:20:02
【问题描述】:

我正在尝试用 php 和 mysql 制作相册系统。 我有列出所有相册的代码:

SELECT  albums.*, count(pictures.id) AS countpic 
FROM albums
LEFT JOIN pictures ON (albums.id=pictures.album_id)
GROUP BY albums.id 
ORDER BY albums.date DESC");

我想列出带有照片缩略图的相册标题(thumb_id si 写在表格图片中)以及分配给每个相册的所有图片的数量。

你不知道如何一键选择thumb_id以及相册中所有图片的数量吗?

谢谢

【问题讨论】:

    标签: php sql mysql select count


    【解决方案1】:

    如果每个专辑都有一张 is_thumb=1 的图片,这将有效。

    SELECT
      albums.*, 
      count(pictures.id) AS countpic,
      group_concat(pictures.thumb_id) ids,
      group_concat(case when picture.is_thumb then pictures.thumb_id else '' end separator '') thumb
    FROM albums
    LEFT JOIN pictures ON (albums.id=pictures.album_id)
    GROUP BY albums.id 
    ORDER BY albums.date DESC");
    

    【讨论】:

      【解决方案2】:

      group_concat(pictures.thumb_id) 添加到选择列表中是否满足您的需要?

      【讨论】:

      • 谢谢..当我想使用随机图片作为相册缩略图时没关系..但是如果我想选择特定图片怎么办? (例如有条件的图片:picture.is_thumb='1')
      • @Bajlo - 请参阅 ceteras 的答案。否则,您可以使用所需输出的示例更新您的问题。
      猜你喜欢
      • 1970-01-01
      • 2021-02-08
      • 1970-01-01
      • 2013-03-20
      • 2017-09-15
      • 2010-12-14
      • 2012-01-10
      • 2011-10-17
      • 1970-01-01
      相关资源
      最近更新 更多