giancy9 giancy9 - 19 days ago 7
SQL Question

select where not in array from multiple values in table

I have an array with some values. I want to retrieve something from a table where the values are not equal to the ones in the array

$myarray_example = array(1.1,2.5);


Table example:

id value
1 1.10
2 1.10
3 2.50
4 2.50
5 3.10
6 3.10


So in this example I want to get only 3.10 value

The query

SELECT value FROM table
WHERE value NOT IN ($myarray_example)


It returns everything. If I use 'WHERE value IN..' then it returns nothing.

Does anyone know why this happen?

Answer
$query = " SELECT value FROM table ";
$query .= " WHERE value NOT IN ( ";

$count = 0;
foreach($myarray as $item) {
    $query .= " . $item . ";
    if ($count != count($myarray) - 1)
        $query .= ",";
    $count++;
}

$query .= ")";
Comments