0

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 ?

0

2 Answers 2

2

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 
1
  • 2
    +1. You may also find it's more efficient to use union all to avoid potential duplicate removal where duplicates probably cannot exists (eg, if id is unique key).
    – paxdiablo
    Commented Apr 20, 2014 at 10:31
1

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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.