lilsizzo
lilsizzo

Reputation: 366

Using union and then arrange the data with order by from second row onwards

lets i have this query

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

and it will result in

1 2 3 4 5

But now i have another query

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

I would like the second query to return

4 1 2 3 5

The result is that way because the query before the union will return "4" and after that i would love to rearrange the result according to seq ascending. Is there a way to arrange the result of a query based on second or Nth row onwards? Is there such a technique to combine both union and order to get this result? if there isnt , could someone show me another idea. by just using a single query. I know theres and alternative by splitting both query out. I just wanna know in this sense, is it possible. THanks in advance.

Upvotes: 0

Views: 161

Answers (1)

Nicola Cossu
Nicola Cossu

Reputation: 56387

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

Upvotes: 1

Related Questions