MySQL - How to use SELECT UNION with different ORDER BY statements?

A/B

Joined
Jan 29, 2023
Messages
7
Reaction score
1
I have the following query:
(SELECT Url, Ref, Text, DateTime, 'C1' AS Source FROM Tbl WHERE Category = 'C1') UNION (SELECT Url, Ref, Text, DateTime, 'C2' AS Source FROM Tbl WHERE Category = 'C2') UNION (SELECT Url, Ref, Text, DateTime, 'C3' AS Source FROM Tbl 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 Tbl 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.
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,792
Messages
2,569,639
Members
45,353
Latest member
RogerDoger

Latest Threads

Top