Re: SQL "OR" Problem - Mailing list pgsql-novice

From Tom Lane
Subject Re: SQL "OR" Problem
Date
Msg-id 22514.1125088067@sss.pgh.pa.us
Whole thread Raw
In response to SQL "OR" Problem  (<operationsengineer1@yahoo.com>)
List pgsql-novice
<operationsengineer1@yahoo.com> writes:
> WHERE t_emp.pos_id = t_pos.pos_id
> AND t_inspect.inspect_emp_id = t_emp.emp_id
> AND t_pos.pos = 'Assembler'
> OR t_pos.pos = 'Quality Inspector'
> OR t_pos.pos = 'Test Technician'

You probably want some parentheses with that:

WHERE t_emp.pos_id = t_pos.pos_id
      AND t_inspect.inspect_emp_id = t_emp.emp_id
      AND (t_pos.pos = 'Assembler'
           OR t_pos.pos = 'Quality Inspector'
           OR t_pos.pos = 'Test Technician')

I believe AND binds more tightly than OR by default, so your original
means

WHERE (t_emp.pos_id = t_pos.pos_id
       AND t_inspect.inspect_emp_id = t_emp.emp_id
       AND t_pos.pos = 'Assembler')
      OR t_pos.pos = 'Quality Inspector'
      OR t_pos.pos = 'Test Technician'

which is unlikely to be what you want.

            regards, tom lane

pgsql-novice by date:

Previous
From: Philip Hallstrom
Date:
Subject: Re: SQL "OR" Problem
Next
From: Stephan Szabo
Date:
Subject: Re: SQL "OR" Problem