SQL联盟按联盟排序

时间:2014-04-20 10:25:52

标签: sql sorting sql-order-by union

我有查询

SELECT id, name 
  FROM users 
  WHERE id !=2 
UNION 
SELECT id, name 
  FROM users2 
  WHERE id != 3;

我希望那种类型,1联盟命令+ 2联合它可能吗?

2 个答案:

答案 0 :(得分:2)

上添加要订购的列
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 :(得分:1)

您也可以这样做

(SELECT id, name 
  FROM users 
  WHERE id !=2
ORDER BY id)
UNION ALL
(SELECT id, name 
  FROM users2 
  WHERE id != 3
  ORDER BY id);