5

This should be an easy one, but I am having a moment. Why does ORDER BY with LIKE sort the matching results as a higher value than the non-matching? To get the results I expect I have to mix ASC and DESC on what is otherwise the same data:

create table foo (name text);
select name from foo order by name like 'm%' desc, name;
4

2 に答える 2

8

"x" like 'm%' is FALSE; "motorcade" like 'm%' is TRUE; "FALSE" < "TRUE".

于 2012-04-16T17:37:25.960 に答える
0

you can use union for this...

select name from foo where name like 'm%' 
order by name desc
union all 
select name from foo where name not like 'm%' 
order by name asc

it will give result as per your requirement.. :)


added asc in second union select query ... just for more understandable statement ... without this(ASC) also query should give desired result..

于 2012-04-16T17:37:24.553 に答える