Skip site navigation (1) Skip section navigation (2)

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 (view raw or flat)
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

pgsql-novice by date

Next:From: Steve CrawfordDate: 2005-08-26 20:29:09
Subject: Re: SQL "OR" Problem
Previous:From: Stephan SzaboDate: 2005-08-26 20:24:50
Subject: Re: SQL "OR" Problem

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group