Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2012
    Location
    Nassau Bahamas, Chelsea Quebec
    Posts
    3

    Unanswered: 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.

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    WHERE city IN ('New York','Chicago','Denver','LA')
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Feb 2012
    Location
    Nassau Bahamas, Chelsea Quebec
    Posts
    3
    Thanks. I totally forgot about IN. I owe you a beer or something.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •