In below SQL query I need 25 rows containing product = ‘y’ and 25 rows having product = ‘n’. Product is a column having Y/N value only. In limit of random 50 rows I need 25 rows from each values.
x
SELECT qno
FROM `wp_question_bank`
where brand='XYZ'
and department='DOMESTIC'
and product = 'y'
and priority = 'p1'
ORDER BY RAND() limit 50
Kindly assist. I have tried Union but it didn’t work.
Advertisement
Answer
Simply use UNION
for each product type:
(SELECT qno
FROM `wp_question_bank`
WHERE brand='XYZ' AND department='DOMESTIC' AND product = 'y' AND priority = 'p1'
ORDER BY RAND()
LIMIT 25)
UNION ALL
(SELECT qno
FROM `wp_question_bank`
WHERE brand='XYZ' AND department='DOMESTIC' AND product = 'n' AND priority = 'p1'
ORDER BY RAND()
LIMIT 25)