6

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?

1 Answer 1

16

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' );
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.