I have problems with ordering the results of a query that joins 4 different tables with UNION ALL operator.
What I have tried is something like this:
SELECT * FROM (
SELECT charge, payment, date FROM table_1
UNION ALL
SELECT charge, payment, date FROM table_2
UNION ALL
SELECT charge, payment, date FROM table_3
UNION ALL
SELECT charge, payment, date FROM table_4
) balance
ORDER BY date ASC;
when I order by certain parameters it works fine, but there are others that return the data ordered by sections instead of as a whole
When I order by date it works as it should, like this:
charge | payment | date
------------------------------
10 | x | 2018-03-23
2 | x | 2018-10-20
8 | x | 2019-03-06
30 | x | 2019-05-10
6 | x | 2019-11-10
15 | x | 2020-07-16
11 | x | 2020-09-20
but when I order by another parameter, in this case "chage", it orders like this:
charge | payment | date
------------------------------
10 | x | 2018-03-23
6 | x | 2019-11-10
30 | x | 2019-05-10
8 | x | 2019-03-06
15 | x | 2020-07-16
11 | x | 2020-09-20
6 | x | 2019-11-10
Sorry if I don't explain myself well, English is not my first language.