MySQL Como múltiples valores
Tengo esta consulta MySQL.
Tengo campos de base de datos con este contenido
sports,shopping,pool,pc,games
shopping,pool,pc,games
sports,pub,swimming, pool, pc, games
¿Por qué este tipo de consulta no funciona? Necesito los campos con deportes o pub o ambos?
SELECT * FROM table WHERE interests LIKE ('%sports%', '%pub%')
7 answers
La lista (a,b,c)
solo funciona con in
. Para like
, tienes que usar or
:
WHERE interests LIKE '%sports%' OR interests LIKE '%pub%'
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-11-13 11:10:32
Forma más rápida de hacer esto:
WHERE interests LIKE '%sports%' OR interests LIKE '%pub%'
Es esto:
WHERE interests REGEXP 'sports|pub'
Encontrado esta solución aquí: http://forums.mysql.com/read.php?10,392332,392950#msg-392950
Más sobre REGEXP aquí: http://www.tutorialspoint.com/mysql/mysql-regexps.htm
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
2011-09-27 09:31:44
¿Por qué no intentas REGEXP? Pruébalo así:
SELECT * FROM table WHERE interests REGEXP 'sports|pub'
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-01-16 11:38:56
Su consulta debe ser SELECT * FROM `table` WHERE find_in_set(interests, "sports,pub")>0
Lo que entiendo es que usted almacena los intereses en un campo de su mesa, lo cual es una idea errónea. Definitivamente deberías tener una tabla de "intereses".
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-23 09:54:29
No olvide usar paréntesis si usa esta función después de un parámetro AND
Así:
WHERE id=123 and(interests LIKE '%sports%' OR interests LIKE '%pub%')
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-05-08 03:00:53
Como @Alexis Dufrenoy propuso, la consulta podría ser:
SELECT * FROM `table` WHERE find_in_set('sports', interests)>0 OR find_in_set('pub', interests)>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
2018-05-09 08:32:06
También puedes usar RLIKE
.
Por ejemplo:
SELECT * FROM TABLE_NAME WHERE COLNAME RLIKE 'REGEX1|REGEX2|REGEX3'
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-05-09 08:32:31