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;
4

1 回答 1

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
于 2012-05-08T13:27:54.850 回答