¿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.
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 '%'
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%'
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). _ci
significa insensible a mayúsculas y minúsculas.
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>%')
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();
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%';
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
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;
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
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
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 :)
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
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