Skip to main content
Inspiring
March 9, 2013
Answered

SQL query - combining AND and OR

  • March 9, 2013
  • 1 reply
  • 1149 views

I am just tweaking a page here:

http://www.goodsafariguide.net/africanhorseback/index102.php

Basically just changing the nominations history part to just show nominations from 2013 and 2011, as some of the palces had too long a list of nominations.

So the original SQL query was:

SELECT NominationID, Category, Year, Finalist, Winner, LodgeID FROM nominations WHERE LodgeID = 288 ORDER BY Year DESC, CATEGORY ASC

So I want it to now display nominations for that LodgeID, but just for 2011 and 2013, which I thought should just be:

SELECT NominationID, Category, Year, Finalist, Winner, LodgeID FROM nominations WHERE LodgeID = 288 AND (Year='2013' OR '2011') ORDER BY Year DESC, CATEGORY ASC

But that isn't working. I've tried a few variations, with and without the inverted commas, but can't seem to get it.

Any suggestions?

This topic has been closed for replies.
Correct answer Luke-Optima

Try this:

SELECT NominationID, Category, Year, Finalist, Winner, LodgeID FROM nominations WHERE (Year='2013' OR Year='2011') AND LodgeID = '288' ORDER BY Year DESC, CATEGORY ASC

if you wanted a run of years like 2011 , 2012 and 2013 you could use:

SELECT NominationID, Category, Year, Finalist, Winner, LodgeID FROM nominations WHERE Year BETWEEN '2011' AND '2013' AND LodgeID = '288' ORDER BY Year DESC, CATEGORY ASC

1 reply

Luke-OptimaCorrect answer
Participating Frequently
March 9, 2013

Try this:

SELECT NominationID, Category, Year, Finalist, Winner, LodgeID FROM nominations WHERE (Year='2013' OR Year='2011') AND LodgeID = '288' ORDER BY Year DESC, CATEGORY ASC

if you wanted a run of years like 2011 , 2012 and 2013 you could use:

SELECT NominationID, Category, Year, Finalist, Winner, LodgeID FROM nominations WHERE Year BETWEEN '2011' AND '2013' AND LodgeID = '288' ORDER BY Year DESC, CATEGORY ASC

Inspiring
March 9, 2013

Thank you! I didn't think to reverse the order like that.

The reason there's no 2012 is because the 2011 awards were in Nov 11, then the next ones were Jan 2013. So they've kind of skipped a year.

Participating Frequently
March 9, 2013

look closer ... it's not just the order!

i have  (Year='2013' OR Year='2011') you have????