Vuoi farlo:
select * from
(
SELECT id, 2 as ordered FROM a -- returns 1,4,2,3
UNION
SELECT id, 1 as ordered FROM b -- returns 2,1
)
order by ordered
Aggiorna
Ho notato che anche se hai due tabelle diverse, unisci gli ID, ciò significa che se hai 1 in entrambe le tabelle, ottieni solo un'occorrenza. Se questo è il comportamento desiderato, dovresti attenerti a UNION . In caso contrario, cambia in UNION ALL .
Quindi noto anche che se modifichi il codice che ti ho proposto, inizieresti a ricevere entrambi 1 e 2 (da entrambi a e b ). In tal caso, potresti voler cambiare il codice proposto in:
select distinct id from
(
SELECT id, 2 as ordered FROM a -- returns 1,4,2,3
UNION
SELECT id, 1 as ordered FROM b -- returns 2,1
)
order by ordered