postgresql 带有 LIKE 和 NOT EQUAL TO 的 Postgres 查询

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

Postgres Query with LIKE and NOT EQUAL TO

postgresql

提问by nateM

I'm trying to write a query with LIKE and != conditions:

我正在尝试使用 LIKE 和 != 条件编写查询:

SELECT * 
FROM   posts 
WHERE  title LIKE 'term%' 
  OR   NAME LIKE 'term%' 
 AND   post_type != 'type'; 

However, the query results are not being filtered by post_type. Is there something wrong with my syntax?

但是,查询结果没有被 post_type 过滤。我的语法有问题吗?

回答by Juan Carlos Oropeza

You probably need parenthesis because ANDhas operator precedence.

您可能需要括号,因为AND具有运算符优先级。

SELECT * 
FROM   posts 
WHERE  ( title LIKE 'term%' OR NAME LIKE 'term%' )
  AND    post_type != 'type';

Because right now without parenthesis you have

因为现在没有括号,你有

SELECT * 
FROM   posts 
WHERE  title LIKE 'term%' 
  OR   (       NAME LIKE 'term%' 
         AND   post_type != 'type' );