Forum Moderators: coopster

Message Too Old, No Replies

Conditions in WHERE clause of SQL statement

         

ironik

10:12 pm on Jun 7, 2005 (gmt 0)

10+ Year Member



I'm having a little trouble with an SQL statement. It's a fairly simple one, but I can't get the conditions after the WHERE clause to behave properly. Here's the statement:

SELECT * FROM mytable_items AS i LEFT JOIN mytable_types AS t ON i.type_id=t.type_id WHERE i.item_flag='1' OR i.item_flag='0' AND i.item_visible='1' AND i.type='test' ORDER BY i.type_id ASC, i.item_cost ASC

What it should do is pull all visible items of the type 'test', and `item_flag` of 1 or 0. What its doing is getting items with a flag of 1 and then optionally ignoring the rest of the conditions because I said 'OR'.

What I think I would like to do is to somehow encapsulate the OR to only affect the item_flag fields... in php I would write it with brackets, but I'm not sure of the right syntax to use with an SQL statement. If I was writing it in php I would probably want it something like this:

if (($item_flag==0 ¦¦ $item_flag ==1) && $item_visible==1 && $type=='test')

Hope I'm making sense. Can anyone tell me the correct syntax to use to help compartmentalise the WHERE clause logic? (It's probably something as simple as brackets, but I haven't found the answer yet).

Dijkgraaf

10:22 pm on Jun 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I would think just brackets around the OR clauses would do the trick.

SELECT * FROM mytable_items AS i LEFT JOIN mytable_types AS t ON i.type_id=t.type_id WHERE (i.item_flag='1' OR i.item_flag='0') AND i.item_visible='1' AND i.type='test' ORDER BY i.type_id ASC, i.item_cost ASC

ironik

10:26 pm on Jun 7, 2005 (gmt 0)

10+ Year Member



Cheers, thanks, I'll give it a go. Wasn't sure of my syntax ;)