使用union,然后从第二行开始按顺序排列数据

时间:2011-03-04 09:09:29

标签: mysql syntax union

让我有这个查询

select * from items where status = 'A' and staffid = '$var1' and workid != '$var2' order by seq asc

,它将导致

1 2 3 4 5

但现在我有另一个查询

select * from items  where status = 'A' and staffid = '$var1' and workid = '$var2'
UNION
select * from items  where status = 'A' and staffid = '$var1' and workid != '$var2' order by seq asc

我想要第二个查询返回

4 1 2 3 5

结果就是这样,因为联合之前的查询将返回“4”,之后我想根据seq升序重新排列结果。有没有办法根据第二行或第N行开始排列查询结果?是否有这样的技术将联合和顺序结合起来得到这个结果?如果没有,有人可以告诉我另一个想法。只需使用一个查询。通过拆分两个查询,我知道了这些和替代方案。我只是想在这个意义上知道,是否有可能。提前谢谢。

1 个答案:

答案 0 :(得分:1)

select *,1 as tab from items  where status = 'A' and staffid = '$var1' and workid = '$var2'
UNION
select *,2 from items  where status = 'A' and staffid = '$var1' and workid != '$var2' order by tab,seq