0

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?

giancy9
  • 31
  • 10
  • Because by your example array, these look like `string`/`varchar` values, and `1.1` <> `1.10` and `2.5` <> `2.50`, thus, you get every row. – Siyual Nov 16 '16 at 17:03
  • Hello, just to clarify, I am passing numbers. It was just a mistake in my post. Both numbers in array and in table are float – giancy9 Nov 16 '16 at 17:04

1 Answers1

1
$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 .= ")";
lubilis
  • 3,942
  • 4
  • 31
  • 54