2012-05-08 105 views
0

我有一個包含2種不同類型(圖片和視頻)的表格。我想獲得每種類型的最多2行...對此有任何幫助?所以結果集將是4行。按類型限制查詢結果

這是查詢我目前所面對的(這只是得到的1型行;圖片):

SELECT DISTINCT 
    mm.mm_id, 
    mm.mm_title, 
    mm.mm_hash 
FROM 
    boomla_multimedia mm, 
    boomla_multimedia_domain md 
WHERE mm.mm_id = md.mm_id 
    AND cat_id = 4 
    AND md.dom_id = 26 
    AND mm.mm_published = 1 
    AND mm.mm_media_type = 'image' 
ORDER BY mm.mm_id DESC 
LIMIT 0, 2; 
+1

請使用格式化選項('{}')使它下次更易於閱讀;) – Nanne

回答

3

讓兩個單獨的查詢,並使用兩個結果集的UNION。

SELECT DISTINCT mm.mm_id, mm.mm_title, mm.mm_hash FROM boomla_multimedia mm, 
    boomla_multimedia_domain md 
WHERE mm.mm_id = md.mm_id AND cat_id = 4 AND md.dom_id = 26 AND mm.mm_published = 1 
    AND mm.mm_media_type = 'image' ORDER BY mm.mm_id DESC LIMIT 0, 2 

UNION 

-- add here the select for 'video' type