'LIKE ('%this%' OR '%that%') and some=else'不工作

发布于 2024-11-17 22:19:01 字数 323 浏览 4 评论 0原文

我有一个选择查询,我试图在字符串中搜索多个模式

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

2024-11-24 22:19:01

如果可以的话那就太好了,但是您不能在 SQL 中使用该语法。

试试这个:

(column1 LIKE '%this%' OR column1 LIKE '%that%') AND something = else

注意括号的使用!您需要将它们放在 OR 表达式周围。
如果没有括号,它将被解析为 A OR (B AND C),这不会给您期望的结果。

It would be nice if you could, but you can't use that syntax in SQL.

Try this:

(column1 LIKE '%this%' OR column1 LIKE '%that%') AND something = else

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.

小姐丶请自重 2024-11-24 22:19:01

不要使用 LIKE,而是使用 REGEXP。例如:

SELECT * WHERE value REGEXP 'THIS|THAT'
mysql> SELECT 'pi' REGEXP 'pi|apa';                     -> 1
mysql> SELECT 'axe' REGEXP 'pi|apa';                    -> 0
mysql> SELECT 'apa' REGEXP 'pi|apa';                    -> 1
mysql> SELECT 'apa' REGEXP '^(pi|apa)

参考:
http://dev.mysql.com/doc/refman/5.1/en /regexp.html

; -> 1 mysql> SELECT 'pi' REGEXP '^(pi|apa)

参考:
http://dev.mysql.com/doc/refman/5.1/en /regexp.html

; -> 1 mysql> SELECT 'pix' REGEXP '^(pi|apa)

参考:
http://dev.mysql.com/doc/refman/5.1/en /regexp.html

; -> 0

参考:
http://dev.mysql.com/doc/refman/5.1/en /regexp.html

Instead of using LIKE, use REGEXP. For example:

SELECT * WHERE value REGEXP 'THIS|THAT'
mysql> SELECT 'pi' REGEXP 'pi|apa';                     -> 1
mysql> SELECT 'axe' REGEXP 'pi|apa';                    -> 0
mysql> SELECT 'apa' REGEXP 'pi|apa';                    -> 1
mysql> SELECT 'apa' REGEXP '^(pi|apa)

Refer:
http://dev.mysql.com/doc/refman/5.1/en/regexp.html

; -> 1 mysql> SELECT 'pi' REGEXP '^(pi|apa)

Refer:
http://dev.mysql.com/doc/refman/5.1/en/regexp.html

; -> 1 mysql> SELECT 'pix' REGEXP '^(pi|apa)

Refer:
http://dev.mysql.com/doc/refman/5.1/en/regexp.html

; -> 0

Refer:
http://dev.mysql.com/doc/refman/5.1/en/regexp.html

柒夜笙歌凉 2024-11-24 22:19:01

尝试如下:

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

憧憬巴黎街头的黎明 2024-11-24 22:19:01

LIKE 子句分解为 2 个单独的语句,即:

(fieldname1 LIKE '%this%' or fieldname1 LIKE '%that%' ) and something=else

Break out the LIKE clauses into 2 separate statements, i.e.:

(fieldname1 LIKE '%this%' or fieldname1 LIKE '%that%' ) and something=else
燃情 2024-11-24 22:19:01

你有什么反对分裂的吗?

...FROM <blah> 
   WHERE 
     (fieldA LIKE '%THIS%' OR fieldA LIKE '%THAT%') 
     AND something = else

Do you have something against splitting it up?

...FROM <blah> 
   WHERE 
     (fieldA LIKE '%THIS%' OR fieldA LIKE '%THAT%') 
     AND something = else
深海少女心 2024-11-24 22:19:01

你有没有尝试过:

(column LIKE '%this%' and something=else) or (column LIKE '%that%' and something=else)

Have you tried:

(column LIKE '%this%' and something=else) or (column LIKE '%that%' and something=else)
×纯※雪 2024-11-24 22:19:01

我知道这是一个有点老的问题,但人们仍然试图找到有效的解决方案,所以你应该使用 全文索引(从 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 inside match ... against ... it took only 0.05s.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文