giancy9
giancy9

Reputation: 41

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?

Upvotes: 0

Views: 1357

Answers (1)

lubilis
lubilis

Reputation: 4160

$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 .= ")";

Upvotes: 1

Related Questions