Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL从表B左连接多行_Mysql - Fatal编程技术网

MySQL从表B左连接多行

MySQL从表B左连接多行,mysql,Mysql,下表是一个示例表结构 我试图编写的查询类似于: SELECT * FROM jobs LEFT JOIN assigned ON jobs.id = assigned.job_id; 但是,正如您所看到的,对于job 100,左连接将生成多个匹配项,但是上面的查询将只返回一个匹配项 是否可以将结果连接到左连接的逗号分隔字符串中 如果可能,进一步的步骤是,是否可以将assigned.user_id替换为连接的逗号分隔字符串中的users.name列 用户 ╔═══╦════════════╦══

下表是一个示例表结构

我试图编写的查询类似于:

SELECT * FROM jobs LEFT JOIN assigned ON jobs.id = assigned.job_id;
但是,正如您所看到的,对于job 100,左连接将生成多个匹配项,但是上面的查询将只返回一个匹配项

是否可以将结果连接到左连接的逗号分隔字符串中

如果可能,进一步的步骤是,是否可以将assigned.user_id替换为连接的逗号分隔字符串中的users.name列

用户

╔═══╦════════════╦═════════════╗
║   ║ id         ║ name        ║
╠═══╬════════════╬═════════════╣
║ 1 ║ 1          ║ Matt        ║
║ 2 ║ 2          ║ Phil        ║
║ 3 ║ 3          ║ Chris       ║
╚═══╩════════════╩═════════════╝
╔═══╦════════════╦═════════════╗
║   ║ id         ║ name        ║
╠═══╬════════════╬═════════════╣
║ 1 ║ 100        ║ Do this     ║
║ 2 ║ 101        ║ Do that     ║
║ 3 ║ 102        ║ And this    ║
╚═══╩════════════╩═════════════╝
工作

╔═══╦════════════╦═════════════╗
║   ║ id         ║ name        ║
╠═══╬════════════╬═════════════╣
║ 1 ║ 1          ║ Matt        ║
║ 2 ║ 2          ║ Phil        ║
║ 3 ║ 3          ║ Chris       ║
╚═══╩════════════╩═════════════╝
╔═══╦════════════╦═════════════╗
║   ║ id         ║ name        ║
╠═══╬════════════╬═════════════╣
║ 1 ║ 100        ║ Do this     ║
║ 2 ║ 101        ║ Do that     ║
║ 3 ║ 102        ║ And this    ║
╚═══╩════════════╩═════════════╝
分配的

╔═══╦════════════╦═════════════╗
║   ║ user_id    ║ job_id      ║
╠═══╬════════════╬═════════════╣
║ 1 ║ 1          ║ 100         ║
║ 2 ║ 2          ║ 100         ║
║ 3 ║ 1          ║ 101         ║
╚═══╩════════════╩═════════════╝

当我为单个作业在末尾添加where子句时,这可能会重复,但如果没有where子句,它只返回一个结果,而不是所有作业。通过在查询末尾添加“GROUP by jobs.id”来解决此问题--