ZeroThe2nd ZeroThe2nd - 2 months ago 8
MySQL Question

Execute multiple SQL queries and sort the output

Currently, I'm working on a project that requires me to execute 2 SQL queries, then I have to sort the output by date and id before writing the output of both queries to a table.

Sorting both queries in SQL with :

ORDER BY order.date DESC, order.id DESC.


Then appending the results in one variable that gets looped over by another part of the script. But executing the seperate queries will result in 2 blocks of sorted items that will not be sorted together when appending the results.

What would be a solution to get the data sorted properly?

Answer

Try to use

SELECT A.* FROM ((QUERY1) UNION ALL (QUERY2)) A ORDER BY A.date DESC, A.id DESC
Comments