views:

38

answers:

2

I ran a query that resulted in the string '1,2,3,4'.

How can I run a second query that treats that string as a list of numbers. So I'll be able to do:

select * from tbl where name not in (1,2,3,4)

I would like an answer in pure MySQL.

+5  A: 

Well first of all, this usually means that your database structure is not good; you should normalize your database.

However, you can do what you want, with the FIND_IN_SET function:

SELECT * FROM tbl WHERE NOT FIND_IN_SET(name, '1,2,3,4')
reko_t
+1 On the normalization. Sounds like the OP really should be able to say SELECT * FROM tbl WHERE name NOT IN (subquery).
mwittrock
+2  A: 

Use FIND_IN_SET:

select * from tbl where FIND_IN_SET(name, '1,2,3,4') = 0

Like the other answer, I would also recommend normalizing your database if at all possible. This query could be slow as it will require a scan of the table. Even if there is an index on name this query won't be able to use it efficiently.

Mark Byers