Question: I have a list of ID's, comma separated stored as string.

123,456,789

I was previously using something like this to pull data for each row in the database where each ID was present:

```
SELECT id, name FROM line_item WHERE id IN (123,456,789)

```

However, I want to only determine the ID's where they do not exist in the database. E.g. check the table to return all ID's provided that do not exist in the DB.

I tried `NOT IN` assuming it would work, but of course, all I got was every ID that did not match my sample, which was massive (thousands of rows, small number of ID's that we typically need to check for).

```
SELECT id FROM line_item WHERE id NOT IN (123,456,789)

```

For example:

Table: ID

```
111
222
333
444
555
777
999

```

String:

```
111,222,666,888,999

```

Query should return:

```
666,888

```

Using MySQL, can I find those values not present without using PHP.

Answer: 
You can create additional table populated with desired `id` values, and use query with LEFT JOIN and WHERE clauses -

```
CREATE TABLE temp_table(id INT);
INSERT INTO temp_table VALUES(123),(456),(789);

SELECT t1.id FROM temp_table t1
  LEFT JOIN line_item t2
    ON t1.id = t2.id
WHERE t2.id IS NULL;

```

Another variant:

```
SELECT t1.id FROM (SELECT 123 id UNION SELECT 456 UNION SELECT 789) t1
  LEFT JOIN line_item t2
    ON t1.id = t2.id
WHERE t2.id IS NULL;

```
`NOT IN` will take the values in the parentheses and cause the query to, as you said, give you all other ids.

I do not know if there is a way to do this with several numbers simultaneously - but this is how you could do it with one:

```
SELECT id, name, description
FROM line_item
WHERE 1 NOT IN (SELECT id FROM line_item) // Replace 1 with your numbers in a loop

```