SQL query trick

I need to do

select * from xxx where name in (a,b,c...);

but I want the result set to be in order (a,b,c...). is it possible?

+3
source share
4 answers

I found this question that looks like your original question: Order in order of values ​​in SQL IN () clause

+3
source

ah - I see. you can do something terrible with the case argument and then arrange it. You would add another column to your query as an “order”, which you could then “arrange by”

its ugly, but if you control the request and the number in the in clause is low, it can work (the beleive condition in is limited to 255 characters)

e.g "IF name = a then 1 else if name = b then 2"

, , (, "in" ).

-ACE

0

.

Oracle - :

SELECT * FROM xxx 
where name in (a,b,c...)
ORDER BY DECODE(name,a,1,b,2,c,3);
0

IN , , .

:

SELECT x.* 
FROM xxx as x 
    INNER JOIN ((select a as name, 1 as ord)
                UNION
                (select b as name, 2 as ord)
                UNION
                (select c as name, 3 as ord)) as t
        ON t.name = x.name
ORDER BY t.ord

, sql. ord . , SqlServer, ROWINDEX, .

0

Source: https://habr.com/ru/post/1749109/


All Articles