DEV Community

Dimitrios Desyllas
Dimitrios Desyllas

Posted on

How I can order the returned results by relevance in mysql or mariadb when I use like statement in where?

As I ask upon https://dba.stackexchange.com/q/338422/118215

I search in this table:

mytable:
---
id PK Biginteger Autoincrement
name varchar(255)
Enter fullscreen mode Exit fullscreen mode

Like this:

select * from mytable where name like "%someval%"; 
Enter fullscreen mode Exit fullscreen mode

And I want to sort my result using the closest match towards someval. How I can do this?

Top comments (2)

Collapse
 
ccoveille profile image
Christophe Colombier

If you were using a MySQL full search index with MATCH, maybe.

Here you are using "LIKE" so it's an exact match modulo the collation.

Maybe you could look around SOUNDEX.

But I'm unsure you will be able to sort then.

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay