Mysql where clause variable help

Discussion in 'MySQL' started by tin2mon, Sep 6, 2010.

  1. #1
    how do i run a query for a variable with multiple values?

    i've been told a loop is not the right way to do it, which is the way i've been doing it and it has worked. I assumed putting it in a foreach loop would accomplish looping the query for variable with multiple values.

    e.g. testa and testb are both in the array, $key.
    foreach($key as $value) {

    //mysql query

    }


    How do i run a single query not in a loop that will have both 'testa' and 'testb' in the WHERE clause?
     
    tin2mon, Sep 6, 2010 IP
  2. rockweb

    rockweb Greenhorn

    Messages:
    60
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #2
    Why not use AND/OR etc to extend the query performance. I think this will run faster and reduce the looping.
     
    rockweb, Sep 6, 2010 IP
  3. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    Not entirely sure on how you are needing to query but you can use an IN clause.

    SELECT columns FROM my_table WHERE column IN ('variable_1','variable_2',etc...);
     
    jestep, Sep 7, 2010 IP