Results 1 to 10 of 10
  1. #1

    mysql select 'or'

    i need to select anything from a mysql table where category= one of several things
    ie something like:

    where cat=1 or cat=2 or cat=fun

    something like that if it makes sense
    workstations&servers @
    http://www.advancedproprietarysystems.com

  2. #2
    Join Date
    Oct 2004
    Location
    Brisbane, Australia
    Posts
    259
    SELECT * FROM table_name WHERE cat = # or cat = #

    You can use ( and ) in SQL statements like: (cat = 'a' AND cat = 'b') OR (cat = 'c' AND cat = 'd')

    Also, it depends on what type of data you are using in the statement where the # are. For example, if the column contains int's, just go cat = 1, otherwise if the column is string data etc use cat = 'text here'

  3. #3
    Join Date
    Dec 2004
    Location
    Canada
    Posts
    1,082

  4. #4
    just saw the 'in' deal, i am gonna stick with or code now that i have it working, it seems easy enough.

    thanks much both!
    Last edited by variable; 07-26-2005 at 03:22 AM.
    workstations&servers @
    http://www.advancedproprietarysystems.com

  5. #5
    Join Date
    Jan 2003
    Posts
    1,715
    Although OR code may work, keep in mind that it severely hampers your ability to index the query, which is crucial for performance.
    Game Servers are the next hot market!
    Slim margins, heavy support, fickle customers, and moronic suppliers!
    Start your own today!

  6. #6
    so 'in' is going to be faster than 'or'?

    i could not get in to work i tried:
    WHERE user='$user' AND cat IN ( 1, 2, 'fun')

    and it did not work.
    workstations&servers @
    http://www.advancedproprietarysystems.com

  7. #7
    Join Date
    Jul 2002
    Location
    Manchester, UK
    Posts
    774
    Looks like the problem there is you're mixing numbers and a string. try:

    WHERE user='$user' AND cat IN ('1','2','fun')

  8. #8
    Join Date
    Jan 2003
    Posts
    1,715
    Yeah, it may not like the mixed types. An actual error message would help, though.
    Game Servers are the next hot market!
    Slim margins, heavy support, fickle customers, and moronic suppliers!
    Start your own today!

  9. #9
    Join Date
    Jul 2005
    Posts
    256
    Although OR code may work, keep in mind that it severely hampers your ability to index the query, which is crucial for performance.
    Actually recent versions of MySQL Query Optimizer (starting from 4.0 I believe) are able to detect ORs and use range select when possible, just like when using IN(...).

  10. #10
    oh, cool then i will stick
    workstations&servers @
    http://www.advancedproprietarysystems.com

Posting Permissions

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