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?

2
  • 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. Commented Nov 16, 2016 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 Commented Nov 16, 2016 at 17:04

1 Answer 1

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 .= ")";
Sign up to request clarification or add additional context in comments.

1 Comment

Hello thanks. I have used something similar to what you just suggested and it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.