I have a mysql result that looks like this:
| compare | field1 | field2 | much more |
-----------------------------------------
| new | x1 | y | ... |
| old | x1 | y | ... |
| old | x2 | y | ... |
| new | x2 | y | ... |
The Query searches for differences of two tables (new and old) and orders it by field1 (with lots more fields, but lets make it simple here). So, the query is returning everything correct but one thing: In the query, there is a UNION ALL with FIRST fetch old, then UNION ALL it to SECOND new.
What i want to have is a result, where my "compare" has ALWAYS FIRST an old and then the new. But see my result, sometimes i get new first and this will break the rest of the code.
I tried it with
ORDER BY FIELD(compare, 'old', 'new'), ...
but it did not work.
Anyone with an idea? Result should be like this:
| compare | field1 | field2 | much more |
-----------------------------------------
| old | x1 | y | ... |
| new | x1 | y | ... |
| old | x2 | y | ... |
| new | x2 | y | ... |
Edit #1:
Here is the complete Query for this:
SELECT
MIN(compareTables) AS 'table',
CONCAT(ObjNr, PFKt, WhgNr, DatVon, AdrNr, KEY_Firma) AS 'primary',
ObjNr,
PFKt,
WhgNr,
DatVon,
DatBis,
AdrNr,
KEY_Firma,
Info,
PKto,
FI_MieterPKto,
PFktArtEigMiet,
ST_SchlAustriaPKto,
inaktiv,
inaktivBis
FROM ((SELECT
'old' AS compareTables,
ObjNr,
PFKt,
WhgNr,
DatVon,
DatBis,
AdrNr,
KEY_Firma,
Info,
PKto,
FI_MieterPKto,
PFktArtEigMiet,
ST_SchlAustriaPKto,
inaktiv,
inaktivBis
FROM table_old.PF_PFkt) UNION ALL (SELECT
'new' AS compareTables,
ObjNr,
PFKt,
WhgNr,
DatVon,
DatBis,
AdrNr,
KEY_Firma,
Info,
PKto,
FI_MieterPKto,
PFktArtEigMiet,
ST_SchlAustriaPKto,
inaktiv,
inaktivBis
FROM table_new.PF_PFkt)) AS compareTables
WHERE 'primary' IS NOT NULL
GROUP BY ObjNr,
PFKt,
WhgNr,
DatVon,
DatBis,
AdrNr,
KEY_Firma,
Info,
PKto,
FI_MieterPKto,
PFktArtEigMiet,
ST_SchlAustriaPKto,
inaktiv,
inaktivBis
HAVING COUNT(*) = 1
ORDER BY ObjNr, PFKt, WhgNr, DatVon, AdrNr, KEY_Firma