按类型限制查询结果

时间:2012-05-08 13:26:26

标签: mysql

我有一个包含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 个答案:

答案 0 :(得分: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