[英]How to fetch unique records from database?
這是我的表結構
album
id ---- albumname
1 General
3 Sports
9 Activities
gallery
id --- ablumid--- image_thumb -- image_full
1 1 uploads/1358401616.jpg uploads/thumbs/thumb_1358401616.jpg
2 1 uploads/1358401641.jpg uploads/thumbs/thumb_1358401641.jpg
3 1 uploads/1358402071.jpg uploads/thumbs/thumb_1358402071.jpg
4 3 uploads/1358402334.jpg uploads/thumbs/thumb_1358402334.jpg
問題是我的畫廊太多了。
這是我的mysql查詢
SELECT DISTINCT gl.catid as gallery_cat_id, ab.albumname,gl.imagest
FROM albums as ab
INNER JOIN gallery as gl ON ab.id=gl.catid
order by ab.albumname ASC
這是結果
gallery_id albumname image_full
38 Activities uploads/thumbs/thumb_1358424428.jpg
41 Activities uploads/thumbs/thumb_1358424483.jpg
40 Activities uploads/thumbs/thumb_1358424468.jpg
39 Activities uploads/thumbs/thumb_1358424446.jpg
該查詢顯示了畫廊表中的所有記錄,我在查詢中添加了DISTINCT,但無法正常工作。 誰能告訴我我在這里想念的嗎?
如果每個相冊只需要一個圖像(無論如何),則可以使用GROUP BY子句按相冊ID進行分組:
SELECT gl.catid as gallery_cat_id, ab.albumname, gl.imagest
FROM
albums as ab
INNER JOIN gallery as gl
ON ab.id=gl.catid
GROUP BY ab.id
order by ab.albumname ASC
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.