Jesper Veldhuizen
Jesper Veldhuizen

Reputation: 602

Limit query result by type

I have a table with 2 different types (image and video). I would like to get max 2 rows of each type... any help on this? So that the resultset will be 4 rows.

This is the query I have at the moment (this just get's the rows from 1 type; image):

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;

Upvotes: 0

Views: 58

Answers (1)

JotaBe
JotaBe

Reputation: 39055

Make two separate queries and use a UNION of the two result sets.

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

Upvotes: 3

Related Questions