GROUP_CONCAT 有多个连接 - 我错过了什么?

GROUP_CONCAT with multiple joins - what am I missing?

我意识到这是一个很常见的问题,所以我花了很多时间查看各种答案,但没有一个能帮助解决我的问题。

我在 PHP / MySQL 中有一个工作请求系统,但我在 GROUP_CONCAT 中遇到了问题。

这是我的架构:

工作
job_id
job_title

工人
worker_id
worker_name

工作地图
workermap_id
job_id
worker_id

一个 table 包含工作,一个工作人员 table 用于可能的工作人员,一个工作人员用于将工作人员映射到工作。一个工作可以有一个或多个工人,一个工人可以有一个或多个工作。

记录工作时,它会在 job 中创建一个条目,但在 workermap 中不会创建任何条目,直到有人专门分配工人。这意味着当我查询作业时,我使用左连接进行查询,因为 workermap 中没有每个作业的条目:

SELECT 
job.job_id,
job.job_title,
workermap.worker_id,
worker.worker_name

FROM job

LEFT JOIN workermap
ON job.job_id = workermap.job_id

LEFT JOIN worker
ON workermap.worker_id = worker.worker_id

问题是,如果有多个工人分配给一个工作,那么这个 returns 多行:

+--------+-----------------------------+-------------+
| job_id | job_title                   | worker_name |
+--------+-----------------------------+-------------+
|    1   | New homepage                | Alan        |
|    1   | New homepage                | John        |
|    2   | Redesign footer             | John        |
|    2   | Redesign footer             | Sarah       |
|    3   | Update contact page content | NULL        |
+--------+-----------------------------+-------------+

我要的是这个:

+--------+-----------------------------+-------------+
| job_id | job_title                   | worker_name |
+--------+-----------------------------+-------------+
|    1   | New homepage                | Alan, John  |
|    2   | Redesign footer             | John, Sarah |
|    3   | Update contact page content | NULL        |
+--------+-----------------------------+-------------+

我知道我需要使用 CONCAT 将工作人员姓名连接在一起,但如果我这样做:

SELECT 
job.job_id,
job.job_title,
GROUP_CONCAT(worker.worker_name)

FROM job

LEFT JOIN workermap
ON job.job_id = workermap.job_id

LEFT JOIN worker
ON workermap.worker_id = worker.worker_id

然后我得到所有工人串联的一行:

+--------+--------------+----------------------+
| job_id | job_title    | worker_name          |
+--------+--------------+----------------------+
|    1   | New homepage | Alan,John,John,Sarah |
+--------+--------------+----------------------+

我试过以下方法:

SELECT 
job.job_id,
job.job_title,
workermap.worker_id,
workermap.worker_list

FROM job

LEFT JOIN 
    (
        SELECT workermap.worker_id, GROUP_CONCAT(worker_name) AS worker_list
        FROM workermap
        INNER JOIN worker
        ON workermap.worker_id = worker.worker_id
    )
AS workermap
SELECT 
job.job_id,
job.job_title,
(SELECT GROUP_CONCAT(worker.worker_name) 
FROM worker
WHERE worker.worker_id = workermap.worker_id)
AS combinedworkers

FROM job

LEFT JOIN workermap
ON job.job_id = workermap.job_id
SELECT 
job.job_id,
job.job_title,
workermap.worker_id,
worker.worker_list

FROM job

LEFT JOIN workermap
ON job.job_id = workermap.job_id

LEFT JOIN 
    (
        SELECT worker_id, GROUP_CONCAT(worker_name) AS worker_list
        FROM worker
        GROUP BY worker_id
    )
AS worker
ON workermap.worker_id = worker.worker_id

None 其中让我更接近了。我确定它相对简单,但我无法解决。谢谢!

这没有你想象的那么复杂。您可以简单地在 (job_id, job_title)GROUP_CONCAT():

上使用聚合
SELECT 
    j.job_id,
    j.job_title,
    GROUP_CONCAT(w.worker_name ORDER BY w.worker_name) worker_names
FROM job j
LEFT JOIN workermap wm ON job.job_id = wm.job_id
LEFT JOIN worker w ON wm.worker_id = w.worker_id
GROUP BY
    j.job_id,
    j.job_title