Wednesday 5 September 2018

MySQL does not display the shortest result first

We have a standard MySQL 5.1 database with a table named places. We have two columns, name and name_bg, where name is the english name of the place and name_bg is the bulgarian name of the place.

We have two places that starts with "РИМ". "Рим" and "Римини".
+--------+--------+--------------+
| id     | name   | name_bg      |
+--------+--------+--------------+
| 221543 | Rimini | Римини       |
|  34514 | Rome   | Рим          |
+--------+--------+--------------+

When searching like this:
select id, name from places where name like 'рим%';

Or like this:
select id, name from places where name_bg like 'рим%';

Everything looks fine.
The weird stuff is when searching like this (the GROUP BY clause):
SELECT places.name, places.name_bg, places.country_id, countries.continent_id WHERE places.name LIKE 'рим%' OR places.name_bg LIKE 'рим%' ORDER ("CHAR_LENGTH(places.name), CHAR_LENGTH(places.name_bg), countries.continent_id, places.popular DESC") GROUP BY country_id LIMIT 10

The query returns Римини first instead of Рим.
We have tryed order and group on the query but nothing helped.
Even switched the IDs of the places but the same result occured.
We want to have the shortes result first.
Any help is appriciated.
Best, Yavor

It's not clear to me why you expect that it should. Does the length function do what you need?
SELECT id,
       name
FROM   places
WHERE  name_bg LIKE 'рим%'
        OR name LIKE 'рим%'
ORDER  BY Length(name)

0 comments:

Post a Comment