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 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();
This is your solution:
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 profileId
FROM profile profile4
WHERE profile4.profilePublishDate <= Now()
ORDER BY profile4.profilePublishDate DESC
LIMIT 0,40
)
ORDER BY RAND()
LIMIT 0,40
)
) TEMP
ORDER BY RAND();
The changes that I've made are:
profile3
for your 2nd queryORDER BY RAND()
, you must create the UNION resultset to a derived table; I have given it TEMP
as the aliasI haven't tested the above query but I hope it should work. Let me know your findings.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With