Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

SQL query - combining AND and OR

Enthusiast ,
Mar 09, 2013 Mar 09, 2013

Copy link to clipboard

Copied

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?

TOPICS
Server side applications

Views

923
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Explorer , Mar 09, 2013 Mar 09, 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

Votes

Translate
Explorer ,
Mar 09, 2013 Mar 09, 2013

Copy link to clipboard

Copied

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

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Mar 09, 2013 Mar 09, 2013

Copy link to clipboard

Copied

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.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 09, 2013 Mar 09, 2013

Copy link to clipboard

Copied

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

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

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Mar 09, 2013 Mar 09, 2013

Copy link to clipboard

Copied

LATEST

D'oh! Wood for trees and all that. Thanks again.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines