Cadena de consulta MySQL contiene
He estado tratando de averiguar cómo puedo hacer una consulta con MySQL que comprueba si el valor (string $haystack
) en una determinada columna contiene ciertos datos (string $needle
), así:
mysql_query("
SELECT *
FROM `table`
WHERE `column`.contains('{$needle}')
");
En PHP, la función se llama substr($haystack, $needle)
, así que tal vez:
WHERE substr(`column`, '{$needle}')=1
7 answers
Bastante simple en realidad:
mysql_query("
SELECT *
FROM `table`
WHERE `column` LIKE '%{$needle}%'
");
El %
es un comodín para cualquier carácter. Tenga en cuenta que esto puede ser lento en conjuntos de datos muy grandes, por lo que si su base de datos crece, necesitará usar índices de texto completo.
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-04-08 17:56:16
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-04-08 19:40:22
WHERE `column` LIKE '%$needle%'
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
2015-12-25 16:40:26
El mío está usando LOCATE
en mysql:
LOCATE(substr,str), LOCATE (substr,str,pos)
Esta función es segura para varios bytes, y solo distingue entre mayúsculas y minúsculas si al menos un argumento es una cadena binaria.
En su caso:
mysql_query("
SELECT * FROM `table`
WHERE LOCATE('{$needle}','column') > 0
");
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
2015-08-21 10:45:12
Además De la respuesta de @WoLpH.
Al usar la palabra clave LIKE
también tiene la capacidad de limitar qué dirección coincide la cadena. Por ejemplo:
Si usted estaba buscando una cadena que comienza con su $needle
:
... WHERE column LIKE '{$needle}%'
Si usted estaba buscando una cadena que termina con el $needle
:
... WHERE column LIKE '%{$needle}'
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
2015-08-21 10:41:57
Tenga en cuenta que esto es peligroso:
WHERE `column` LIKE '%{$needle}%'
Hacer primero:
$needle = mysql_real_escape_string($needle);
Así evitará posibles ataques.
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-27 15:20:25
Usted probablemente está buscando find_in_set
función:
Where find_in_set($needle,'column') > 0
Esta función actúa como in_array
función en PHP
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
2015-12-03 17:08:34