'LIKE ('%this%' OR '%that%') and some=else'不工作
我有一个选择查询,我试图在字符串中搜索多个模式
LIKE ('%this%' or '%that%' ) and something=else
返回零结果
但是
LIKE '%this%' and something=else
返回结果 并
LIKE '%that%' and something=else
返回结果
是否可以将我的所有结果放入一个查询中?如果一个字符串两者都匹配,它将如何处理?
I have a select query where I am trying to search strings for multiple patterns
LIKE ('%this%' or '%that%' ) and something=else
Returns zero results
However
LIKE '%this%' and something=else
returns results
and
LIKE '%that%' and something=else
returns result
Is it possible to get all my results into one query? If a string matches both, how will it handle that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果可以的话那就太好了,但是您不能在 SQL 中使用该语法。
试试这个:
注意括号的使用!您需要将它们放在
OR
表达式周围。如果没有括号,它将被解析为
A OR (B AND C)
,这不会给您期望的结果。It would be nice if you could, but you can't use that syntax in SQL.
Try this:
Note the use of brackets! You need them around the
OR
expression.Without brackets, it will be parsed as
A OR (B AND C)
,which won't give you the results you expect.不要使用
LIKE
,而是使用REGEXP
。例如:参考:
http://dev.mysql.com/doc/refman/5.1/en /regexp.html
Instead of using
LIKE
, useREGEXP
. For example:Refer:
http://dev.mysql.com/doc/refman/5.1/en/regexp.html
尝试如下:
WHERE(column LIKE '%this%' OR column LIKE '%that%')AND some = else
Try something like:
WHERE (column LIKE '%this%' OR column LIKE '%that%') AND something = else
将
LIKE
子句分解为 2 个单独的语句,即:Break out the
LIKE
clauses into 2 separate statements, i.e.:你有什么反对分裂的吗?
Do you have something against splitting it up?
你有没有尝试过:
Have you tried:
我知道这是一个有点老的问题,但人们仍然试图找到有效的解决方案,所以你应该使用 全文索引(从 MySQL 5.6.4 开始提供)。
在 where 块中通过三重
like
对包含超过 3500 万条记录的表进行查询需要 ~2.5s,但在这些字段上添加索引并使用 布尔模式内匹配...反对...
它花了仅有的0.05s。I know it's a bit old question but still people try to find efficient solution so instead you should use FULLTEXT index (it's available from MySQL 5.6.4).
Query on table with +35mil records by triple
like
in where block took ~2.5s but after adding index on these fields and using BOOLEAN MODE insidematch ... against ...
it took only 0.05s.