MySQL 你如何 OR 两个 LIKE 语句?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/240582/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 12:17:24  来源:igfitidea点击:

How do you OR two LIKE statements?

sqlmysql

提问by Thomas Owens

I have tried the following two statements:

我尝试了以下两个语句:

  • SELECT col FROM db.tbl WHERE col (LIKE 'str1' OR LIKE 'str2') AND col2 = numresults in a syntax error
  • SELECT col FROM db.tbl WHERE page LIKE ('str1' OR 'str2') AND col2 = numresults in "Truncated incorrect DOUBLE value: str1" and "Truncated incorrect DOUBLE value: str2" for what looks like every result. However, no results are actually returned.
  • SELECT col FROM db.tbl WHERE col (LIKE 'str1' OR LIKE 'str2') AND col2 = num导致语法错误
  • SELECT col FROM db.tbl WHERE page LIKE ('str1' OR 'str2') AND col2 = num导致“截断不正确的 DOUBLE 值:str1”和“截断不正确的 DOUBLE 值:str2”,看起来像每个结果。但是,实际上没有返回任何结果。

I figured one of the two statements would work, but they aren't.

我认为这两个陈述之一会起作用,但它们不是。

回答by Tom Ritter

SELECT col FROM db.tbl WHERE (col LIKE 'str1' OR col LIKE 'str2') AND col2 = num
SELECT col FROM db.tbl WHERE (col LIKE 'str1' OR col LIKE 'str2') AND col2 = num

回答by warren

I believe you need WHERE ((page LIKE 'str1') OR (page LIKE 'str2'))

我相信你需要 WHERE ((page LIKE 'str1') OR (page LIKE 'str2'))

回答by Eclipse

The "OR" operator applies to expressions. "LIKE 'str1'" and "LIKE 'str2'" are not valid expressions, since the "LIKE" operator requires both a left hand side and a right hand side.

“OR”运算符适用于表达式。"LIKE 'str1'" 和 "LIKE 'str2'" 不是有效的表达式,因为 "LIKE" 运算符需要左侧和右侧。

page LIKE ('str1' OR 'str2')

would first try to OR together 'str1' and 'str2', giving the value TRUE. It would then try and evaluate page LIKE TRUE, and report an error.

将首先尝试将 'str1' 和 'str2' 进行 OR 运算,得到值 TRUE。然后它会尝试评估page LIKE TRUE,并报告错误。

回答by Daniel Spiewak

Think simple:

想得简单:

SELECT col FROM db.tbl WHERE (col LIKE 'str1' OR col LIKE 'str2') AND col2 = ...

回答by Elie

try SELECT col FROM db.tbl WHERE (col LIKE 'str1' or col LIKE 'str2') AND col2 = num

尝试 SELECT col FROM db.tbl WHERE (col LIKE 'str1' or col LIKE 'str2') AND col2 = num

回答by Developer

USE %at the Start and End of the String. So that it will check the Sub-Strings as well

%在字符串的开头和结尾使用 USE 。这样它也会检查子字符串

SELECT col FROM db.tbl WHERE (col LIKE '%str1%' OR col LIKE '%str2%') AND col2 = num