[英]selecting data from two different columns in one table that point to the same column in another table
I have two tables which i have summarised below just to keep the question simple.我有两个表格,我在下面总结了这些表格,只是为了让问题简单。
The first table has a list of tasks - the two important columns to note is the id number "idTask" and the name of that task "sgItemName".第一个表有一个任务列表 - 需要注意的两个重要列是 id 号“idTask”和该任务的名称“sgItemName”。 It looks like this
看起来像这样
idTask ![]() |
sgItemName ![]() |
---|---|
1 ![]() |
Do this![]() |
2 ![]() |
Do that![]() |
3 ![]() |
Then this![]() |
4 ![]() |
Then that![]() |
The second table lists each task that has a precedent task that needs to be completed before it, and then what that precedent is - the two important columns to note is the parent task "fkidParentTask" and the precedent task "fkidPrecedentTask".第二个表列出了具有需要在其之前完成的先例任务的每个任务,然后是该先例 - 要注意的两个重要列是父任务“fkidParentTask”和先例任务“fkidPrecedentTask”。 Some tasks might have multiple precedents.
有些任务可能有多个先例。 It looks like this
看起来像这样
fkidParentTask ![]() |
fkidPrecedentTask ![]() |
---|---|
2 ![]() |
1 ![]() |
3 ![]() |
2 ![]() |
3 ![]() |
1 ![]() |
4 ![]() |
3 ![]() |
4 ![]() |
2 ![]() |
What i would like to achieve is a select query that gives me an output as follows我想要实现的是一个 select 查询,它给了我一个 output 如下
Parent Task![]() |
Precedent Task![]() |
---|---|
Do that![]() |
Do this![]() |
Then this![]() |
Do that![]() |
Then this![]() |
Do this![]() |
Then that![]() |
Then this![]() |
Then that![]() |
Do that![]() |
I'm still learning SQL but I cannot get this to work at all?我仍在学习 SQL 但我根本无法让它工作? Can someone help me?
有人能帮我吗?
Thank you in advance!先感谢您!
You need to use the table1
multiple times in the join as follows:您需要在join中多次使用
table1
,如下所示:
select t11.sgItemName as ParentTask,
t12.sgItemName as PrecedentTask
from table2 t2
join table1 t11 on t2.fkidParentTask = t1.idTask
join table1 t12 on t2.fkidPrecedentTask = t1.idTask
For SQL-Server:对于 SQL-Server:
Select tm1.sgItemName as [Parent Task] , tm2.sgItemName [Precedent Task]
from ParentPrecedentMapping ppm
join Taskmaster tm1 on ppm.fkidParenttask = tm1.idtask
join Taskmaster tm2 on ppm.fkidPrecedenttask = tm2.idtask
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.