Search code examples
sqlboolean-logicboolean-operationsbooleanquery

SQL Parenthesis


I'm having trouble with a SQL expression that doesn't do what I thought it should do.

I have jobs that belong to clients and also that jobs may pertain to a certain project or either the project would be null (if they pertain to a project in the column project_id it appears an int).

First of all I'm taking all jobs that are available but I need to remove certain clients when the project is null.

I'm doing this in the WHERE clause:

AND (p4_.uid NOT IN (722, 4736, 1041, 735) AND s3_.project_id IS NULL)

But SQL is taking it as two separated AND like this

AND p4_.uid NOT IN (722, 4736, 1041, 735) 
AND s3_.project_id IS NULL

And for example is not showing any from the client with uid 735 even if the client have jobs with a project id assigned.


Solution

  • but I need to remove certain clients when the project is null

    This requirement should be written as:

    AND NOT (p4_.uid IN (722,4736,1041,735) AND s3_.project_id IS NULL)
    

    or the equivalent:

    AND (p4_.uid NOT IN (722,4736,1041,735) OR s3_.project_id IS NOT NULL)