MySQL Forums
Forum List  »  Newbie

How to use SELECT UNION with different ORDER BY statements?
Posted by: aSettantuno Uno
Date: March 24, 2023 03:24PM

I have the following query:

(SELECT
Url, Ref, Text, DateTime, 'C1' AS Source
FROM Table
WHERE Category = 'C1')

UNION

(SELECT
Url, Ref, Text, DateTime, 'C2' AS Source
FROM Table
WHERE Category = 'C2')

UNION

(SELECT
Url, Ref, Text, DateTime, 'C3' AS Source
FROM Table
WHERE Category = 'C3')

ORDER BY DateTime DESC

Now I need to merge another query, that has a different "ORDER BY":

UNION

(SELECT
Url, Ref, Text, Expiration, 'C4' AS Source
FROM Table
WHERE Category = 'C4' AND Expiration >= CURDATE()
ORDER BY Expiration ASC)

I've tried to add it as above, but it doesn't work.

What's wrong?

Thanks in advance.

Options: ReplyQuote


Subject
Written By
Posted
How to use SELECT UNION with different ORDER BY statements?
March 24, 2023 03:24PM


Sorry, only registered users may post in this forum.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.