通過

2012-12-28 45 views
0

訂購後選擇一條記錄我有一堆CD包含一堆藝術品。我想在網頁上顯示每張CD的拇指圖像,爲此,我需要從CD中存在的許多作品中挑選一張圖像...通過

查詢選擇所有藝術作品和我的組按位置,並希望選擇第一個。我不就沒怎麼只選擇第一個圖像的每個記錄後,我命令他們......

查詢是如下:

SELECT release_artwork.release_id, release_artwork.position, artwork.small_filename, artwork.small_width, artwork.small_height 
FROM release_artwork 
JOIN artwork ON artwork.artwork_id = release_artwork.artwork_id 
ORDER BY release_artwork.position 

enter image description here

+0

確定我張貼的查詢 – Marco

+0

您是否在尋找一個只爲每個位置或結果只有一個行集? –

+0

你的問題並不完全清楚。 'release_id'對應一張專輯,還是'位置'?你可以在你想避免重複的列上添加一個「GROUP BY」。 – cryocide

回答

4

我沒有方便的MySQL實例,但這應該工作 - 只需定義一個內聯表來保存最小值,然後加入到那個。

SELECT release_artwork.release_id, release_artwork.position, artwork.small_filename, artwork.small_width, artwork.small_height 
FROM 
    release_artwork 
    inner JOIN artwork ON artwork.artwork_id = release_artwork.artwork_id 
    inner join 
    (
    SELECT 
    ra.release_id as release_id 
    ,min(ra.position) as rap 
    FROM 
    release_artwork ra 
    group by 
    ra.release_id 
) mins 
    on mins.release_id = release_artwork.release_id 
    and mins.rap = release_artwork.position 
ORDER BY 
    release_artwork.release_id 
2

在這種情況下,你可以使用LIMIT 1,將您檢索的結果限制爲僅第一個。

0
SELECT release_artwork.release_id, release_artwork.position, artwork.small_filename, artwork.small_width, artwork.small_height 
FROM release_artwork 
JOIN artwork ON artwork.artwork_id = release_artwork.artwork_id 
ORDER BY release_artwork.position LIMIT 1 

在末端添加LIMIT 1將其限定爲單個結果。

+0

爲真。我已經編輯了我的問題,所以我想爲每個release_id限制1行 – Marco