Newsletters older than 6 months may have links that are out of date. Please use the Search to check for updated links.
Imagine you have table with names and scores like
Name Score
Tom 10
John 350
Jim 23
And one day your boss comes and says:
"Combine Top 5 with Bottom 5 rows, add to that 10 random rows and display it in nice table, but no more than 15 rows. Ofcourse result must be sorted by score."
Can you do this with single query?
Yes you can. Just add some brackets around queries.
(SELECT name, score FROM scores ORDER BY score DESC LIMIT 5)
UNION (SELECT name, score FROM scores ORDER BY score LIMIT 5)
UNION (SELECT name, score FROM scores ORDER BY RAND() LIMIT 10)
ORDER BY score DESC LIMIT 15;
P.S. In near future same can be used with Subqueries in MySQL 4.1