tags:

views:

88

answers:

5

I guess this is a long shot but, is there a way to list the sql query without sorting...

eg. I have

select * from items 
where manufacID = 2 
or manufacID = 1
or manufacID = 4

and I don't want them to be listed in the asc or decs order, but as i typed in... so 2,1,4.

So, can i do that?

+5  A: 

Yes, use this:

SELECT * FROM items
WHERE manufacID IN (2, 1, 4)
ORDER BY (manufacID = 2) ASC,
         (manufacID = 1) ASC,
         (manufacID = 4) ASC

The results are sorted in the order that the conditions match. Change ASC to DESC to reverse the order. This only works on databases which allow conditions in the sort clauses.

(Side note: why would you want to do this?)

Thomas O
Cool! Do you really need that ASC? It is default mode, I as remember.
alxx
I was showing that it could be done either way.
Thomas O
This relies on true sorting before false. I'm not sure if SQL has a standard for this. In Postgres, false sorts before true, which means that your example as given would be the reverse of the desired order. You could fix the problem by changing the sort to desc, but it's cryptic. I did this once and put a comment on it to explain, but since then I've switch to using a case/when as more readable and maintainable.
Jay
Now I think about it, ASC is redundant in that it will only alter the true-false sorting order, not the order of the final list. Good point on true-false sorting order - this is undefined on some databases and so should be restricted to single engines.
Thomas O
+7  A: 

You could add an extra column in the select as your sort column then order by that:

SELECT 
  *,
  CASE manufacID
    WHEN 2 THEN 1
    WHEN 1 THEN 2
    WHEN 4 THEN 3
  END AS sortOrder
FROM
  items
ORDER BY
  sortOrder
Caps
I haven't seen Thomas O's answer before, but if it works it looks like a much neater solution. Go for that one!
Caps
+4  A: 

As you haven't specified the sorting, the records will be sorted in the natural order, which depends on the RDBMS you are using. In SQL Server for example the order is undefined.

You can create a value to order by from the values:

select * from items 
where manufacID in (2, 1, 4)
order by case manufacID
  when 2 then 1
  when 1 then 2
  when 4 then 3
end
Guffa
that did the trick!
Andrej
+1  A: 
SELECT * FROM
(
select 1 as sort, * from items 
where manufacID = 2 

union all

select 2 as sort, * from items 
where manufacID = 1

union all

select 3 as sort, * from items 
where manufacID = 4
)
order by sort
edosoft
The sorting shouldn't be necessary when using UNION ALL?
Thorarin
It would be interesting to see what is faster, the unions or the order by solutions.
Kris.Mitchell
A union is a relatively slow operation. While this works, it is almost always faster to use a CASE. Also, while in this case the rest of the query is pretty simple, if you had a complex query, the entire complex query would have to be repeated 3 times. Someone reading it later has to then figure out whether the whole complicated thing is the same or if there are differences. And if someone makes a change, they have to make the same change 3 times. If they make a mistake, only change 2 places or mistype one of the three, you could get subtle errors. I'd avoid this.
Jay
A: 
select * from items where manufacID = 2 
union all
select * from items where manufacID = 1
union all
select * from items where manufacID = 4
gertas