So I have a cover-flow style image carousel populated by a query like
SELECT foo FROM `imageTable` ORDER BY RAND() LIMIT 0, 12;
My client would like to add to this a set of permanent images. Due to the way the results are used it would be very handy if I could make those records part of my original query.
I tried
SELECT foo FROM `imageTable` WHERE `id` = 'x' OR `id` = 'y'
OR `id` IS NOT NULL ORDER BY RAND() LIMIT 0, 12;
I was hoping the records with the ids would necessarily be in the result set but nope the RAND() shuffles the whole set.
So my question is whether any single query could provide the x permanent records and then y random records?
// EDIT WITH REVISED QUERY USING Adrian Serafin’s answer:
(SELECT * FROM `imageTable` ORDER BY RAND() LIMIT 0, 6)
UNION
(SELECT * FROM `imageTable` WHERE `id` = 'foo' OR `id` = 'bar' OR `id` = 'baz')
You can try union command in sql.