If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Multiple 'OR' clauses in SQL query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-01-12, 15:25
TommyBahama TommyBahama is offline
Registered User
 
Join Date: Feb 2012
Location: Nassau Bahamas, Chelsea Quebec
Posts: 3
Multiple 'OR' clauses in SQL query

Hello:

I have a MYSQL database connected to a website where users log in, and name cities that they will travel to in the near future. They can name up to 10 cities nationwide. I take their list of cities and then query a coupon table where coupons are available according to a geographic location.

My question: Is there an easy way to name a group of elements and then do an sql query for any element in the group.

For example MyCityGroup = {'New York', 'Chicago', 'Denver', 'LA'};

And I would like to say something like this:

SELECT * from coupon_table WHERE city IS AN ELEMENT OF MyCityGroup;

Is this possible, or do I have to do the long form:

SELECT * from coupon_table
WHERE city='New York'
OR city='Chicago'
OR city='Denver'
OR city='LA';


Looking for a shortcut if there is one.

Thanks in advance, and I look forward to contributing to this forum.
Reply With Quote
  #2 (permalink)  
Old 02-01-12, 15:36
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
WHERE city IN ('New York','Chicago','Denver','LA')
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 02-01-12, 15:40
TommyBahama TommyBahama is offline
Registered User
 
Join Date: Feb 2012
Location: Nassau Bahamas, Chelsea Quebec
Posts: 3
Thanks. I totally forgot about IN. I owe you a beer or something.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On