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
Author: ks1322, 2010-04-08

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.

 331
Author: Wolph,
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

Uso:

SELECT *
  FROM `table`
 WHERE INSTR(`column`, '{$needle}') > 0

Referencia:

 116
Author: OMG Ponies,
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%'
 41
Author: oedo,
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
");
 20
Author: risnandar,
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}'
 10
Author: Joshua Powell,
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.

 2
Author: Alejandro Moreno,
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

 0
Author: Andres,
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