0

I have the select query with or logical operator like following:

SELECT * FROM grabli_new.product where article like '%AV2%' or article like '%AV22%';

I need to ordering result set by "length of like pattern"(rows that contains the longest pattern in my case '%AV22%' must be in the beginning of result set). The query must return all result that contains '%AV22%' pattern in the beginning and only then all result that contains '%AV2%' pattern. Is it possible?

fidel150992
  • 303
  • 5
  • 17
  • possible duplicate of [Mysql: Order by like?](http://stackoverflow.com/questions/3609166/mysql-order-by-like) – George Oct 13 '14 at 10:36

3 Answers3

1

You can try to use something like this:

SELECT *, case when article like '%AV22%' 
                    then 1 
                    when article like '%AV2%' 
                    then 2 end orderIndex 
FROM grabli_new.product 
where article like '%AV2%' or article like '%AV22%'
order by orderIndex
abilash
  • 897
  • 3
  • 12
  • 32
0

if you want to count (multi-byte) characters, use the CHAR_LENGTH function instead:

ORDER BY CHAR_LENGTH( column )
-1
SELECT * 
FROM grabli_new.product 
WHERE article LIKE '%AV2%' OR article LIKE '%AV22%' 
ORDER BY char_length(article) DESC
Lucas Zamboulis
  • 2,494
  • 5
  • 24
  • 27
knkarthick24
  • 3,106
  • 15
  • 21