sql - MySQL group_concat with where clause -
i got problem group_concat , filter. in table got module names linked client. want search clients module name, in group concat still want see modules owned client. display clients modules, display specific module. can't figure out how make them both work together.
any suggestions on how expected result??
these basic tables , query tried along results , result wanted
client +--------------------+ | id | name | +--------------------+ | 1 | client1 | | 2 | client2 | | 3 | client3 | | 4 | client4 | +--------------------+ module +--------------------+ | id | name | +--------------------+ | 1 | module1 | | 2 | module2 | | 3 | module3 | | 4 | module4 | +--------------------+ client_module +-------------------------+ | client_id | module_id | +-------------------------+ | 1 | 2 | | 1 | 3 | | 2 | 1 | | 2 | 2 | | 2 | 4 | | 3 | 4 | | 4 | 1 | | 4 | 2 | | 4 | 3 | | 4 | 4 | +-------------------------+
query:
select client.id, client.name, group_concat(module.name) modules client left join client_module on client_module.client_id = client.id left join module on module.id = client_module.module.id module.id in (1,2)
results:
received +--------------------------------------------------+ | id | name | modules | +--------------------------------------------------+ | 1 | client1 | module2 | | 2 | client2 | module1,module2 | | 4 | client4 | module1,module2 | +--------------------------------------------------+ expected +------------------------------------------------------+ | id | name | modules | +------------------------------------------------------+ | 1 | client1 | module2,module3 | | 2 | client2 | module1,module2,module4 | | 4 | client4 | module1,module2,module3,module4 | +------------------------------------------------------+
you can try this.
select client.id, client.name, group_concat(module.name) modules client left join client_module on client_module.client_id = client.id left join module on module.id = client_module.module_id group client.id having find_in_set('module1',modules)>0 or find_in_set('module2',modules)>0
Comments
Post a Comment