【问题标题】:group concat and in clause组 concat 和 in 子句
【发布时间】:2015-11-20 12:31:25
【问题描述】:

您好,有两个表分别命名为 Roles 和 jobs

当运行下面的查询时

SELECT job_id ,GROUP_CONCAT(roles_title) from jobs left join roles on roles_id in (roles) group by job_id 

预期结果

<table>
  <tr>
    <td>Job_id</td>
    <td>Roles</td>
  </tr>
  <tr>
    <td>1</td>
    <td>Admin,acc & test</td>
  </tr>
  <tr>
    <td>2</td>
    <td>acc& test</td>
  </tr>
</table>

【问题讨论】:

  • 这里的答案是规范化数据,而如果你有很多行,答案可能会起作用,它会比蜗牛慢。

标签: mysql where group-concat where-in


【解决方案1】:

您可以在 join 子句中使用FIND_IN_SET()

SELECT job_id ,GROUP_CONCAT(roles_title SEPARATOR '&')
     from jobs
      left join roles on FIND_IN_SET(roles_id,roles)
GROUP BY job_id 

希望这会有所帮助。

【讨论】:

  • 我只得到单行
  • 哦,对不起,我忘记分组了。请立即查看
猜你喜欢
  • 2015-07-24
  • 1970-01-01
  • 2010-10-07
  • 2022-12-11
  • 2021-07-20
  • 1970-01-01
  • 1970-01-01
  • 2015-05-07
  • 1970-01-01
相关资源
最近更新 更多