Jump to content

Exclude 1 ID from sql query while using an OR


xXREDXIIIXx

Recommended Posts

Here's my query but The OR is making the != '6' invalid want to be able to include both genres on the 'games page' but obviously not show the same game in the related section

 

SELECT gameid, title FROM games WHERE gameid != '6' AND gen1 = 'First Person Shooter' OR gen2 = 'Online' ORDER BY RAND( ) LIMIT 5

 

  3    Battlefield: Bad Company

4 Battlefield: Bad Company 2

8 Crysis 2

7 Call of Duty 4: Modern Warfare

6 Call of Duty: Modern Warfare 2 <-- This ID should not be included

Basicly, and rightly, he's saying to use brackets:

SELECT gameid, title FROM games WHERE ((gameid != '6' AND gen1 = 'First Person Shooter') OR (gameid != '6' AND gen2 = 'Online')) ORDER BY RAND( ) LIMIT 5

Should do what you want it to.  In the future I would recomend that if you are making any multi-part where clause just bracket it up (you can even put brackets round a single WHERE (a = b) if you fancy it.

Basicly, and rightly, he's saying to use brackets:

SELECT gameid, title FROM games WHERE ((gameid != '6' AND gen1 = 'First Person Shooter') OR (gameid != '6' AND gen2 = 'Online')) ORDER BY RAND( ) LIMIT 5

 

Actually your example would not require brackets any more. It's equivalent to:

2 * (3 + 4) = 2 * 3 + 2 * 4

 

;)

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.