I have a table like below
Create Table tmp_test (
id int(11) unsigned Auto_increment primary key ,
contract_id int(11) unsigned,
item_id int(11) unsigned
);
Insert Into tmp_test (contract_id,item_id)
Values (10,1),(10,2),(10,3),(12,1),(12,2),(14,1),
(16,1),(16,2),(16,3),(16,4),(18,1),(18,2),(20,1),
(20,2),(20,3),(22,2),(22,3),(22,4),(24,1),(24,4);
When we select Query
1. Select Distinct contract_id From tmp_test Where FIND_IN_SET(item_id, '1,2');
OutPut Need to
12, 18
Select Distinct contract_id
From tmp_test
Where FIND_IN_SET(item_id, '1');
OutPut Need to
14
Select Distinct contract_id
From tmp_test
Where FIND_IN_SET(item_id, '1,2,3');
OutPut Need to
10, 20
Select Distinct contract_id
From tmp_test
Where FIND_IN_SET(item_id, '4,1');
OutPut Need to
24
Please help me to achieve this in single query
Regards,
Faisal
Select contract_id From (
Select contract_id,group_concat(item_id order By item_id) items From tmp_test
Group By contract_id ) sb
Where items Like '1,2';