MySQL group_concat with where clause

若如初见. 提交于 2019-12-04 07:48:43

You can Try Like this.

SELECT     client.id, client.name, GROUP_CONCAT(module.name) AS modules
FROM       client
LEFT JOIN  client_module ON client_module.client_id = client.id
LEFT JOIN  module ON module.id = client_module.module_id
group by client.id Having Find_In_Set('module1',modules)>0 or Find_In_Set('module2',modules)>0

SQL Fiddle Demo

You are using client_module.module_id change it to client_module.client_id. Use group by with group_cancat

SELECT     client.id, client.name, GROUP_CONCAT(module.name) AS modules
FROM       client
LEFT JOIN  client_module ON client_module.client_id = client.id
LEFT JOIN  module ON module.id = client_module.module_id
WHERE      client_module.client_id IN (1,2,4)
group by client.id, client.name

fiddle

try that

   SELECT     client.id, client.name, GROUP_CONCAT(module.id) AS modules
   FROM       client
   LEFT JOIN  client_module ON client_module.client_id = client.id

   LEFT JOIN  module ON module.id = client_module.module_id
   WHERE      client.id IN (1,2,4) 
   group by client.id
  • You have this wrong client_module.module.id fixed to client_module.module_id

  • AND you are already saying to your WHERE clause to return only 1 and 2 .

  • Added Group by to work when you have Group_Concat

DEMO HERE

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!