SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot jooles's Avatar
    Join Date
    Jun 2006
    Posts
    144
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Querying Based On Many Conditions?

    I have a database table with a field "category" that has many different possibilities (100+). I'm trying to write a query that grabs the rows that have "category" that has one of 60ish category possibilities. Currently the query looks like this:

    Code MySQL:
    SELECT * FROM table WHERE 
     category = 1 OR
     category = 2 OR 
     category = 3 OR 
     ...
     category = 58 OR 
     category = 59 OR 
     category = 60
    ;

    This seems enormously inefficient. Is there a better way to do this? Can I somehow store all of the category values I'm looking for in an array, and just compare once? Any help would be much appreciated, thanks!

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,047
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    SELECT * FROM table WHERE category IN (1,2,3,58,59,60)

  3. #3
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,015
    Mentioned
    53 Post(s)
    Tagged
    2 Thread(s)
    Code:
    SELECT t.somecolumns
      FROM daTable AS t
    INNER
      JOIN category_possibilities AS c
        ON c.category = t.category
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Bookmarks

Posting Permissions

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