group concat和in clause

时间:2015-11-20 12:31:25

标签: mysql where group-concat where-in

您有两个名为角色和工作的表格jobs table structure

Roles table structure

并在运行时查询以下

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

Result i got

预期结果

<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>

1 个答案:

答案 0 :(得分:0)

您可以在加入条款中使用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 

希望这有帮助。