Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres Query with LIKE and NOT EQUAL TO

Tags:

postgresql

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

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?

like image 622
nateM Avatar asked May 16 '16 18:05

nateM


1 Answers

You probably need parenthesis because AND has operator precedence.

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' );
like image 170
Juan Carlos Oropeza Avatar answered Sep 28 '22 02:09

Juan Carlos Oropeza