MySQL 喜欢多个值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4172195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL Like multiple values
提问by webmasters
I have this MySQL query.
我有这个 MySQL 查询。
I have database fields with this contents
我有包含此内容的数据库字段
sports,shopping,pool,pc,games
shopping,pool,pc,games
sports,pub,swimming, pool, pc, games
Why does this like query does not work? I need the fields with either sports or pub or both?
为什么这样的查询不起作用?我需要有运动场或酒吧或两者兼而有之的场地?
SELECT * FROM table WHERE interests LIKE ('%sports%', '%pub%')
采纳答案by Andomar
The (a,b,c)
list only works with in
. For like
, you have to use or
:
该(a,b,c)
列表仅适用于in
. 对于like
,您必须使用or
:
WHERE interests LIKE '%sports%' OR interests LIKE '%pub%'
回答by jazkat
Faster way of doing this:
更快的方法:
WHERE interests LIKE '%sports%' OR interests LIKE '%pub%'
is this:
这是:
WHERE interests REGEXP 'sports|pub'
Found this solution here: http://forums.mysql.com/read.php?10,392332,392950#msg-392950
在这里找到这个解决方案:http: //forums.mysql.com/read.php?10,392332,392950#msg-392950
More about REGEXP here: http://www.tutorialspoint.com/mysql/mysql-regexps.htm
更多关于 REGEXP 的信息:http://www.tutorialspoint.com/mysql/mysql-regexps.htm
回答by Ahmad Hussain
Why not you try REGEXP. Try it like this:
为什么不试试 REGEXP。像这样尝试:
SELECT * FROM table WHERE interests REGEXP 'sports|pub'
回答by iamharish15
You can also use RLIKE
as well.
您也可以使用RLIKE
。
For example:
例如:
SELECT * FROM TABLE_NAME WHERE COLNAME RLIKE 'REGEX1|REGEX2|REGEX3'
回答by Alexis Dufrenoy
Your query should be SELECT * FROM `table` WHERE find_in_set(interests, "sports,pub")>0
您的查询应该是 SELECT * FROM `table` WHERE find_in_set(interests, "sports,pub")>0
What I understand is that you store the interests in one field of your table, which is a misconception. You should definitively have an "interest" table.
我的理解是您将兴趣存储在表格的一个字段中,这是一种误解。您绝对应该有一个“兴趣”表。
回答by Luan
Don't forget to use parenthesis if you use this function after an AND
parameter
如果在AND
参数后使用此函数,请不要忘记使用括号
Like this:
像这样:
WHERE id=123 and(interests LIKE '%sports%' OR interests LIKE '%pub%')
回答by Franc Drobni?
回答by Intacto
More work examples:
更多工作示例:
SELECT COUNT(email) as count FROM table1 t1
JOIN (
SELECT company_domains as emailext FROM table2 WHERE company = 'DELL'
) t2
ON t1.email LIKE CONCAT('%', emailext) WHERE t1.event='PC Global Conference";
Task was count participants at an event(s) with filter if email extension equal to multiple company domains.
如果电子邮件扩展名等于多个公司域,则任务是使用过滤器计算事件的参与者。
回答by vadim
Or if you need to match only the beginning of words:
或者,如果您只需要匹配单词的开头:
WHERE interests LIKE 'sports%' OR interests LIKE 'pub%'
you can use the regexp caret matches:
您可以使用正则表达式插入符匹配:
WHERE interests REGEXP '^sports|^pub'