根据存储在不同 table 中的 ID 选择两个名称
Selecting two names based on ID stored in different table
假设我有两个表:
Table 1: 有列的任务 (id, authorId, assigneeId)
Table 2: 员工列 (id, name)
authorId
和 Table 中的 assigneeId
1 引用了 Table 中的 id
列 2.
如何从这两个表中得到selectTask ID、Author Name和Assignee Name(如果没有assigneeId,则Assignee name默认为'null')?
我不知道如何开始,所以很难。谁能提示如何启动代码?
您可以加入两次:
select t.id, e1.name as author_name, e2.name as assignee_name
from tasks t
left join employees e1 on e1.id = t.author_id
left join employees e2 on e2.id = t.assignee_id
假设我有两个表:
Table 1: 有列的任务 (id, authorId, assigneeId)
Table 2: 员工列 (id, name)
authorId
和 Table 中的 assigneeId
1 引用了 Table 中的 id
列 2.
如何从这两个表中得到selectTask ID、Author Name和Assignee Name(如果没有assigneeId,则Assignee name默认为'null')?
我不知道如何开始,所以很难。谁能提示如何启动代码?
您可以加入两次:
select t.id, e1.name as author_name, e2.name as assignee_name
from tasks t
left join employees e1 on e1.id = t.author_id
left join employees e2 on e2.id = t.assignee_id