D
D
di2020-10-24 14:55:14
PostgreSQL
di, 2020-10-24 14:55:14

Sort by earlier occurrence of a string in a query?

There is such a request. By the string transmitted by the user, we are looking for something in the database

SELECT email FROM users WHERE email ILIKE :query ORDER BY email

We get such a list for the request "test"
SELECT email FROM users WHERE email ILIKE "%test%" ORDER BY email


[email protected]
[email protected]
[email protected]
[email protected]
[email protected]

_
_
_
_ oiu
[email protected]
[email protected]

Is it possible to do this at the sql query level?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
mayton2019, 2020-10-24
@mayton2019

It is necessary to sort by the Levenshtein distance from the "test" expression to the email field.

D
di, 2020-11-01
@Delgus

Postgres has ORDER BY POSITION()
ORDER BY POSITION('test' in email)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question