mysql - 组 concat 和 in 子句

标签 mysql where-clause group-concat where-in

您好,有两个表,分别名为 Roles 和 jobs 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>

最佳答案

您可以在 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 

希望这有帮助。

关于mysql - 组 concat 和 in 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33826832/

相关文章:

mysql - 如何选择具有日期频率的逾期行?

php - 回显 sql 行作为页面 url

python - 当列等于特定值时,将新值插入新列 - Python

mysql - GROUP BY 和 GROUP_CONCAT - 如何返回正确的值?

Mysql连接表结果为一行

sql - 如何在mysql查询中获取一个变量中的多列

mysql - SQL查询结果更改的通知

php - 以 JSON 格式存储多条记录时出错

php - 如何将数组解析为 MYSQL IN()?

mysql - 这可以在不使用 View 的情况下编写,而是使用子查询或其他东西吗