I have query
SELECT id, name
FROM users
WHERE id !=2
UNION
SELECT id, name
FROM users2
WHERE id != 3;
I want that sort will be, 1 union orders + 2 union it's possible ?
Add a column to order on
SELECT id, name, 1 as unionOrder FROM users WHERE id !=2
UNION
SELECT id, name, 2 as unionOrder FROM users2 WHERE id != 3
ORDER BY unionOrder
union all
to avoid potential duplicate removal where duplicates probably cannot exists (eg, if id
is unique key).
Commented
Apr 20, 2014 at 10:31
You can as well do like
(SELECT id, name
FROM users
WHERE id !=2
ORDER BY id)
UNION ALL
(SELECT id, name
FROM users2
WHERE id != 3
ORDER BY id);