and or in query

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

and or in query

Post by shiznatix »

i am making a dynamic select query but what i wanna do is have a and with some or's in it. i wanna do somthing like

Code: Select all

WHERE
  table1 = gwarrrr
AND
  table2 = bloort
AND
  table3 = oneht
AND
  table4 = one
    OR
  table4 = two
    OR
  table4 = three
but i want to not have all those OR 1 of the table4's i want it to be like AND if 1 of those 4's are equal to one of the values. understand?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Thats why you should use brackets... To show us which parts belong together... See the differences?

WHERE a OR b AND c
WHERE a OR (b AND c)
WHERE (a OR b) AND c

So your query would be something like

WHERE a AND (b = val1 OR b = val2 or b = val3)

With the IN operator you can write this as:

WHERE a AND (b IN (1, 2, 3))
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

gotcha, many thanks
Post Reply