1

I have a users table with fields

(id, name, email, companies)

and data as:

(1, 'Dave', 'dave@test.com', '1,3')
(2, 'Jake', 'jake@test.com', '2,3')
(3, 'Peter', 'peter@test.com', '1,2')

Each user can access the companies in their profile. The problem I have is to select the relevant users for a specific company.

For example, I want the users fro company 2, so the result should be 'Jake' and 'Peter'. Getting the result in PHP is pretty easy, just do a normal query to the user's table and explode the companies and check if the company exists. Is there a way to do it in MYSQL?

I've tried SELECT * FROM USERS WHERE companies IN (???) but I can't get my head around how to explode the companies in MYSQL.

Frank AK
  • 1,705
  • 15
  • 28

1 Answers1

3

Assuming that you want the users of company 2.

SELECT * FROM USERS WHERE find_in_set('2', companies) will work for you.