简体   繁体   English

如何在 mysql select 语句中使用 OR LIKE?

[英]How do you use OR LIKE in a mysql select statement?

I'm having trouble with this select statement:我在使用此 select 语句时遇到问题:

Select * from table where row != '' and row2 like '%test%' or like '%test2%';

The "or like" part isn't working, I'm guessing I'm using it incorrectly. “或喜欢”部分不起作用,我猜我使用不正确。

You would do something like this:你会做这样的事情:

SELECT * FROM table
WHERE row != '' AND (row2 like '%test%' OR row2 like '%test2%')

Each OR / AND operate on one thing.每个 OR / AND 对一件事进行操作。 As others have noted, order of operations are important.正如其他人所指出的,操作顺序很重要。 You should group expressions using () so that they are evaluated first.您应该使用()对表达式进行分组,以便首先评估它们。

You miss the "row2" in second like, but also your query like now works like this你错过了第二个喜欢的“row2”,但你现在的查询也像这样工作

Select * from table where row != '' and row2 like '%test%';
Select * from table where row2 like '%test2%';

If you need it like that, it's ok, but I presume you want in both searches the row to be?= ""?如果你需要这样,没关系,但我想你想在两个搜索中都希望该行是?=“”?

If that's the case, than your query should like this:如果是这种情况,那么您的查询应该是这样的:

Select * from table where row != '' and (row2 like '%test%' or row2 like '%test2%');

notice the brackets.注意括号。

There is an operator precedence problem - use parentheses to resolve it:存在运算符优先级问题 - 使用括号来解决它:

SELECT *
  FROM table
 WHERE row != '' 
   AND (row2 LIKE '%test%' OR row2 LIKE '%test2%');

You were also missing the second 'row2' in the OR'd conditions.您还缺少 OR'd 条件中的第二个“row2”。

Of course, the second LIKE condition in the example will not return any rows that the first does not (because every row that contains 'test2' also contains 'test'), but we can assume that was an over-simplification for the purposes of the question.当然,示例中的第二个 LIKE 条件不会返回第一个不返回的任何行(因为包含“test2”的每一行也包含“test”),但我们可以假设这是一个过度简化的目的这个问题。

Where clauses are associated to columns OF a row in a table you are querying from, not a "row" itself. Where 子句与您正在查询的表中的行的列相关联,而不是“行”本身。 So, if dealing with a simple customer table, you could apply LIKE queries something like因此,如果处理一个简单的客户表,您可以应用类似的 LIKE 查询

select 
      lastname,
      firstname,
      address
   from
      customers
   where
         firstname like 'Bill%'
      OR lastname like '%mar%'

will return any names where the first name would specifically START with "Bill" regardless of what it ends with (via the "%" wildcard)将返回任何名字以“Bill”开头的名字,无论它以什么结尾(通过“%”通配符)

OR或者

the last name has the value "mar" anywhere in the last name, such as "Marcus", "Tamara", "Omar"...姓氏在姓氏中的任何位置都有值“mar”,例如“Marcus”、“Tamara”、“Omar”...

Each "LIKE" clause being applied is based on the column you are interested in comparing against.... If you wanted multiple values tested for a single column you would have to write it each time...应用的每个“LIKE”子句都基于您有兴趣比较的列...。如果您想为单个列测试多个值,则每次都必须编写它...

where
   firstname like 'Bill%'
   or firstname like 'John%'
   or firstname like '%any%'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM