2013-01-21 94 views
1

這裏是我的表結構如何從數據庫中獲取唯一記錄?

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 

查詢顯示,從庫表中的所有記錄,我增加查詢不同,但它無法正常工作。 任何人都可以告訴我我錯過了什麼嗎?

+5

這看起來像四個獨特的行我 –

+0

解釋什麼你需要。不同的專輯名稱? – Shomz

+0

查詢的期望結果是什麼? – Taryn

回答

0

如果你只需要一個單一的形象(不管是什麼)的每一張專輯,你可以使用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 
相關問題