Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.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 SQL查询员工和任务表_Mysql_Sql - Fatal编程技术网

Mysql SQL查询员工和任务表

Mysql SQL查询员工和任务表,mysql,sql,Mysql,Sql,我有一个员工和一个任务表,如下所示: Table employees id not null primary key, name 我正在尝试编写一个查询,该查询将返回任务id、作者姓名和受让人姓名,如果没有受让人姓名,那么我希望返回null。仅供参考,这不是一个家庭作业问题,但我正在学习后端开发,我需要提高我的sql技能 更新: 这是预期的输出 -- Expected output (in any order): -- id author assignee -- --------

我有一个员工和一个任务表,如下所示:

Table employees
  id not null primary key,
  name 
我正在尝试编写一个查询,该查询将返回任务id、作者姓名和受让人姓名,如果没有受让人姓名,那么我希望返回null。仅供参考,这不是一个家庭作业问题,但我正在学习后端开发,我需要提高我的sql技能

更新: 这是预期的输出

-- Expected output (in any order):
-- id  author  assignee
-- ----------------------
-- 1   Richard
-- 2   Lily    Richard

您正在寻找两个
left join
s:

select t.*, eau.name as author, eas.name as assignee
from tasks t left join
     employees eau
     on eau.id = t.author_id left join
     employees eas
     on eas.id = t.assignee_id;

你能解释一下吗?另外,如果我要获得预期的输出(请参阅更新)?我该怎么做?@Saad。为什么要将结果放在两行而不是一行中?是否只提供了示例输出而没有任何示例输入?请看
select t.*, eau.name as author, eas.name as assignee
from tasks t left join
     employees eau
     on eau.id = t.author_id left join
     employees eas
     on eas.id = t.assignee_id;