簡體   English   中英

從MySQL表中選擇ID和計數(ID)

[英]selecting id and count(id) from table in mysql

我正在嘗試在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以及相冊中所有圖片的數量嗎?

謝謝

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

如果每個專輯都有一張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");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM