简体   繁体   English

从一个表中的两个不同列中选择数据,这些列指向另一个表中的同一列

[英]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 idTask sgItemName sgItemName
1 1 Do this做这个
2 2 Do that去做
3 3 Then this那么这个
4 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 fkidParentTask fkidPrecedentTask fkidPrecedentTask
2 2 1 1
3 3 2 2
3 3 1 1
4 4 3 3
4 4 2 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.

相关问题 将数据从同一表的其他两列插入一列 - Insert data into one column from two other columns of the same table 单个查询根据不同的条件从同一个表中将一列的数据拆分为两列 [SQL] - Single query to split out data of one column, into two columns, from the same table based on different criteria [SQL] 将数据从一列插入另一表的两列的过程 - Procedure to insert data from one column into two columns in another table 通过从两列中选择值来插入同一表中的一列 - insert by selecting values from two columns into a column from the same table sql两个列的一个表引用另一个表中的同一列 - sql two columns of one table reference same column in another table 将一个表的两列连接到另一表的同一列 - Join two columns of one table to the same column of another table 将一个表中的两个不同列连接到另一个表中的同一列? - Joining two different columns from one table to the same column in a different table? 从两列中选择值并将它们串联到另一张表的一列中 - Selecting values from two columns and concatenating them into a column on a different table 从一个表中获取一列,而从另一表中获取两列 - Get one column from one table with two columns of another table 一个表中的两列是否可以具有另一表中同一列的外键? - Can two columns from one table have a foreign key to the same column in another table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM