¿Cómo puedo buscar (sin distinción de mayúsculas y minúsculas) en una columna usando COMO comodín?


Miré alrededor y no encontré lo que buscaba, así que aquí va.

SELECT * FROM trees WHERE trees.`title` LIKE  '%elm%'

Esto funciona bien, pero no si el árbol se llama Elm o ELM etc...

¿Cómo hago que SQL sea insensible a mayúsculas y minúsculas para esta búsqueda de comodines?

Estoy usando MySQL 5 y Apache.

Author: Genhis, 2010-05-20

12 answers

SELECT  *
FROM    trees
WHERE   trees.`title` COLLATE UTF8_GENERAL_CI LIKE '%elm%'

En realidad, si agregas COLLATE UTF8_GENERAL_CI a la definición de tu columna, puedes omitir todos estos trucos: funcionará automáticamente.

ALTER TABLE trees 
 MODIFY COLUMN title VARCHAR(…) CHARACTER 
 SET UTF8 COLLATE UTF8_GENERAL_CI. 

Esto también reconstruirá cualquier índice en esta columna para que puedan usarse para las consultas sin encabezar '%'

 209
Author: Quassnoi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-02-25 07:24:44

Siempre he resuelto esto usando lower:

SELECT * FROM trees WHERE LOWER( trees.title ) LIKE  '%elm%'
 186
Author: cwallenpoole,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-05-20 18:41:22

La distinción entre mayúsculas y minúsculas se define en la configuración columnas / tablas / intercalación de base de datos. Puede hacer la consulta bajo una intercalación específica de la siguiente manera:

SELECT *
FROM trees
WHERE trees.`title` LIKE '%elm%' COLLATE utf8_general_ci

, Por ejemplo.

(Reemplace utf8_general_ci con cualquier cotejo que encuentre útil). _cisignifica insensible a mayúsculas y minúsculas.

 43
Author: aioobe,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-06-30 06:44:01

Este es el ejemplo de una consulta TIPO simple:

SELECT * FROM <table> WHERE <key> LIKE '%<searchpattern>%'

Ahora, no distingue mayúsculas de minúsculas usando LOWER () func:

SELECT * FROM <table> WHERE LOWER(<key>) LIKE LOWER('%<searchpattern>%')
 34
Author: Federico Piragua,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-07-19 20:50:25

Estoy haciendo algo así.

Obteniendo los valores en minúsculas y MySQL hace el resto

    $string = $_GET['string'];
    mysqli_query($con,"SELECT *
                       FROM table_name
                       WHERE LOWER(column_name)
                       LIKE LOWER('%$string%')");

Y Para MySQL Alternativa PDO:

        $string = $_GET['string'];
        $q = "SELECT *
              FROM table_name
              WHERE LOWER(column_name)
              LIKE LOWER(?);";
        $query = $dbConnection->prepare($q);
        $query->bindValue(1, "%$string%", PDO::PARAM_STR);
        $query->execute();
 14
Author: Doğa Özkaracaabatlıoğlu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-21 09:02:14

Creo que esta consulta hará una búsqueda insensible a mayúsculas y minúsculas:

SELECT * FROM trees WHERE trees.`title` ILIKE '%elm%';
 8
Author: cgupta,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-08-13 15:05:39

Simplemente use:

"SELECT * FROM `trees` WHERE LOWER(trees.`title`) LIKE  '%elm%'";

O Utilizar

"SELECT * FROM `trees` WHERE LCASE(trees.`title`) LIKE  '%elm%'";

Ambas funciones funciona igual

 8
Author: Vi8L,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-01-09 11:50:10

No necesitas ALTER ninguna tabla. Simplemente use las siguientes consultas, antes de la consulta SELECT real que desea usar el comodín:

    set names `utf8`;
    SET COLLATION_CONNECTION=utf8_general_ci;
    SET CHARACTER_SET_CLIENT=utf8;
    SET CHARACTER_SET_RESULTS=utf8;
 1
Author: Lopofsky,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-06-22 09:52:56

Debe configurar la codificación y el cotejo adecuados para sus tablas.

La codificación de la tabla debe reflejar la codificación de datos real. ¿Cuál es la codificación de sus datos?

Para ver la codificación de la tabla, puede ejecutar una consulta SHOW CREATE TABLE tablename

 0
Author: Your Common Sense,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-05-20 18:42:16

Cuando quiero desarrollar búsquedas de mayúsculas insensibles, siempre convierto cada cadena a minúsculas antes de hacer la comparación

 0
Author: Rbacarin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-05-20 19:07:44

Bien en mysql 5.5, como operador es insensitive...so si su vale es elm o ELM o Elm o eLM o cualquier otro , y usa como '%elm%' , mostrará todos los valores coincidentes.

No puedo decir sobre versiones anteriores de mysql.

Si va en Oracle , como trabajar como sensible a mayúsculas , por lo que si escribe como '%elm%' , irá solo para esto e ignorará mayúsculas..

Extraño , pero así es como es :)

 0
Author: user21546,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-04-24 12:29:16
SELECT name 
       FROM gallery 
       WHERE CONVERT(name USING utf8) LIKE _utf8 '%$q%' 
       GROUP BY name COLLATE utf8_general_ci LIMIT 5 
 -1
Author: user4189641,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-10-28 12:52:25