Sort by: Return unLIKE to LIKE?

It should be easy, but I have a moment. Why does ORDER BY with LIKE sort the matching results as a higher value than the mismatch? To get the results, I expect that I will need to mix ASC and DESC with the rest of the data:

create table foo (name text);
select name from foo order by name like 'm%' desc, name;
+5
source share
2 answers

"x", like "m%", is FALSE; A tuple, such as m%, is set to TRUE; "FALSE" <"TRUE".

+8
source

you can use unionfor 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

he will give the result according to your requirement .. :)


asc union... ... (ASC) .

0

All Articles