Re: SQL "OR" Problem

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: operationsengineer1(at)yahoo(dot)com
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: SQL "OR" Problem
Date: 2005-08-26 20:27:47
Message-ID: 22514.1125088067@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

<operationsengineer1(at)yahoo(dot)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

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Steve Crawford 2005-08-26 20:29:09 Re: SQL "OR" Problem
Previous Message Stephan Szabo 2005-08-26 20:24:50 Re: SQL "OR" Problem