0

I have 3 tables

  1. user
  2. service
  3. userServices

User Table

Id  |  Name   | Status
------------------------
1   |  User1  | y
2   |  User2  | y
3   |  User3  | y
4   |  User4  | y

Service Table

Id  |  ServiceName   | Status
------------------------
1   |  Service1      | y
2   |  Service2      | y
3   |  Service3      | y
4   |  Service4      | y

User Service Table

Id  |  UserId   | ServiceId   | Status
-----------------------------------------
1   |  1        | 1           | y
2   |  1        | 3           | y
3   |  1        | 2           | y
4   |  1        | 4           | y
5   |  2        | 2           | y
6   |  2        | 3           | y
7   |  2        | 4           | y
8   |  3        | 1           | y
9   |  3        | 3           | y
10  |  3        | 4           | y
11  |  3        | 2           | y

I will get service id as comma separated string like 1,2,3 as the input & I need to check that which user provide this all services.

For example; User 1 provides services 1,3,2,4, and 1,2,3 is subset of 1,3,2,4 so this record should be returned.

User 2 provide services 2,3,4, so 1,2,3 is not subset of 2,3,4 then this record should not be returned.

I have tried using FIND_IN_SET with GROUP_CONCAT of user's id but it doesn't work.

1 Answers1

0

Can the User Service Table contain duplicates?

If not, you can try

SELECT UserId FROM userServices 
WHERE serviceId IN (1,2,3)
GROUP BY UserId HAVING COUNT(*) >= 3

( replace 1,2,3 with the input and 3 with number of groups in input, or you could even calculate it in the SQL itself)

Yossi Vainshtein
  • 3,845
  • 4
  • 23
  • 39