Help with mysql query checking field against array

Discussion in 'MySQL' started by JEET, May 2, 2009.

  1. #1
    Hi,
    I got a php array:
    $a= array(
    [0]=>5,
    [1]=>2,
    [2]=>10
    );
    I need to get some results from mysql where "id" field is not one of those in array.
    mysql_query(" select * from table where some_field='ok' and id!=' values of array ' ");
    I am not getting the idea on how to make the query so it's quick and gets me desired results.
    Please help.
    Thanks :)
     
    JEET, May 2, 2009 IP
  2. oburlaca

    oburlaca Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    $sql = "SELECT * FROM table WHERE some_field='ok' AND id NOT IN(" . implode(",", array_values($a)) . ')';
    mysql_query($sql);
     
    oburlaca, May 4, 2009 IP
  3. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #3
    Thanks for this. I'll try this and let you know
    Thanks :)
     
    JEET, May 4, 2009 IP
  4. olddocks

    olddocks Notable Member

    Messages:
    3,275
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    215
    #4
    olddocks, May 4, 2009 IP