I am trying to UNION two queries which both contain ORDER BY's. As I have discovered, you can not order by queries that are part of a UNION. I just don't know how else to do this query then. Let me explain what I'm trying to do.
- I am trying to select the 40 most recent profiles and from that list select a random set of 20. I then want to union that with:
- Select 40 random profiles where the profile does not fall in the original 40 most recent profiles queried in the first set
- Randomly order that whole set of 60 records.
I am aware of the efficiency ramifications of using the Rand() function
SELECT profileId
FROM (SELECT profileId
FROM profile profile2
WHERE profile2.profilePublishDate <= Now()
ORDER BY profile2.profilePublishDate DESC
LIMIT 0,40) AS profile1
ORDER BY RAND()
LIMIT 0,20
UNION (SELECT profileId
FROM profile profile4
WHERE profileId NOT IN (SELECT profileId
FROM profile profile4
WHERE profile4.profilePublishDate <= Now()
ORDER BY profile4.profilePublishDate DESC
LIMIT 0,40)
ORDER BY RAND()
LIMIT 0,40) as profile3
ORDER BY RAND()
UPDATE: This is the solution based on Abhay's help below (thanks Abhay):
SELECT *
FROM
(
(
SELECT profileId
FROM
(
SELECT profileId
FROM profile profile2
WHERE profile2.profilePublishDate <= Now()
ORDER BY profile2.profilePublishDate DESC
LIMIT 0,40
) AS profile1
ORDER BY RAND()
LIMIT 0,20
)
UNION
(
SELECT profileId
FROM profile profile4
WHERE profileId NOT IN (
SELECT * FROM
(
SELECT profileId
FROM profile profile4
WHERE profile4.profilePublishDate <= Now()
ORDER BY profile4.profilePublishDate DESC
LIMIT 0,40
) AS temp2
)
ORDER BY RAND()
LIMIT 0,40
)
) TEMP
ORDER BY RAND();