Get values with like mysql

0

I need to make a query that returns only one result, it is the following: among my records I have a field called name and I want the query to return the name that contains cumla with the following:

  • If I say "hello" and in the registers I have: "cat" "hello" and "dog" I must return "hello", however I want to limit it to only having up to 2 different characters, that is if I have "holassssss" or "aaaaahola" would not apply, but if I have "holass" or "aahola" then yes

  • If there is more than one result then return the one with fewer different characters, that is, if the records that match are "holass", "hello" and "hello" then you should return "hello"

  • Ignore if it is uppercase or lowercase

I tried using:

SELECT NOMBRE FROM USUARIO WHERE NOMBRE LIKE '%__hola__%'; 

But as you will notice it does not achieve what was expected, I hope you can help me.

    
asked by gibran alexis moreno zuñiga 09.05.2017 в 19:34
source

2 answers

3

I think the simplest thing would be to use LENGTH :

SELECT NOMBRE
FROM USUARIO
WHERE NOMBRE LIKE '%hola%'
AND LENGTH(NOMBRE) <= LENGTH('Hola') + 2
ORDER BY LENGTH(NOMBRE) 
LIMIT 1;
    
answered by 09.05.2017 / 19:38
source
0

You can try using REGEXP that allows more options to parse a result than what you could get with LIKE. An example:

SELECT NOMBRE FROM USUARIO WHERE REGEXP 'hola' LIMIT 1; 

I suggest you review the available options with REGEXP and choose the one that best suits the search you are doing.

    
answered by 09.05.2017 в 19:47