Skip to content
Advertisement

Retrieve rows in which a column doesn’t contain a value

I have a MySQL table like below:

| ID |  userIDs  
---------------
|  1 | 4,3,5   
|  2 | 2,3
|  3 | 1,2,3

I want to retrieve all the rows in which userIDs doesn’t contain 1. I tried

SELECT * FROM tablename  WHERE 1 NOT IN (userIDs)

But it’s not working.

Advertisement

Answer

Use FIND_IN_SET

SELECT * FROM tablename  
WHERE find_in_set(1, userIDs) = 0

But actually you should rather change your table design. Never store multiple values in a single column!

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement