简体   繁体   中英

selecting id and count(id) from table in mysql

I am trying to make a photo album system in php and mysql. I have this code for list all photo albums:

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");

I want to list title of photo albums with photo thumbnails (thumb_id si written in table pictures) and also with number of all pictures assigned to each albums.

Dont you know how to select thumb_id and also number of all pictures in album by using one select?

Thanks

group_concat(pictures.thumb_id)添加到选择列表是否group_concat(pictures.thumb_id)您的需求?

This would work if for each album there is a single image with 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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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