简体   繁体   English

我如何连接两个表,其中两列引用 SQL 服务器中第二个表中的同一列

[英]How can i join two tables with two columns that refers the same column in the second table in SQL Server

I have two tables to join.我有两张桌子要加入。

First table has two columns第一个表有两列

  1. User ID of the record owner.记录所有者的用户 ID。
  2. User ID of the assigned user.已分配用户的用户 ID。

And the second table has userId and username columns.第二个表有 userId 和 username 列。 These two columns of first table refers to the same column of the same table.第一个表的这两列是指同一个表的同一列。 I want to see both user names in a view but I can't get those at the same time.我想在一个视图中查看两个用户名,但我无法同时获得它们。 How should I do that?我该怎么做?

Edit编辑

select
    TASKID,
    CREATED_BY,
    CREATED_AT,
    ASSIGNED_USER,
    USERNAME ,
    DEADLINE,
    USERNAME
from 
    TASKS
inner join 
    USERS on ASSIGNED_USER = USERID

I can get username of ASSIGNED_USER 's username with the query above.我可以通过上面的查询获得ASSIGNED_USER的用户名的用户名。 I've tried the second for getting both usernames but it didn't work我已经尝试了第二个来获取两个用户名,但它没有用

select
    TASKID,
    CREATED_BY,
    CREATED_AT,
    ASSIGNED_USER,
    USERNAME ,
    DEADLINE,
    USERNAME
from 
    TASKS
inner join 
    USERS on ASSIGNED_USER = USERID and CREATED_BY = USERID

It returns an empty result.它返回一个空结果。

Join USERS twice to decode different references加入 USERS 两次以解码不同的引用

select TASKID, CREATED_BY, CREATED_AT, ASSIGNED_USER, au.USERNAME assignedUser_Name, DEADLINE,  uc.USERNAME createdByUser_Name
from TASKS t
inner join USERS au
on t.ASSIGNED_USER = au.USERID 
inner join USERS uc
on t.CREATED_BY = uc.USERID

you have to join to user twice once for CREATED_BY and once for ASSIGNED_USER:您必须为 CREATED_BY 和一次 ASSIGNED_USER 加入用户两次:

select
    TASKID,
    CREATED_BY,
    CREATED_AT,
    ASSIGNED_USER,
    USERNAME ,
    DEADLINE,
    USERNAME
from TASKS
inner join USERS u1 on ASSIGNED_USER = u1.USERID 
inner join USERS u2 on CREATED_BY = u2.USERID

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何连接两个表,其中一个表中的一列引用其他表中的3列? - How to join two tables where one column in one table refers to 3 columns in other table? SQL Server:如何连接两个表并水平旋转第二个表 - SQL Server : how to join two tables and rotate the second table horizontally postgresql/sql - 加入(双?)两个表并按第二个表的列排序 - postgresql/sql - join(Double?) two tables and sort by a column of the second table 如何将这两个表连接到同一个Sql结果中 - How can I join these two tables into the same Sql result 如何在 SQL 的一个表中使用两个相同的外键命名连接表中的两列? - How to name two columns in join tables with two same foreign key at one table in SQL? 如何连接两个SQL表,将第二个表中的值放入第一个表中的列? - How can I join two SQL tables, putting a value from the 2nd table into a column in the 1st? 当且仅当一个ID链接到SQL Server CE第二个表中的多行时,如何将两个表连接在一起? - How do I join two tables together if and only if an id is linked to multiple rows in the second table in SQL Server CE? 如何使用SQL Server从引用同一表的两个键中联接一个表? - How can I join a table from two keys referencing same table with SQL Server? 如何在 sql 的两个表上加入多个列 - how can I join multiple columns on two table in sql 我可以在一个整数类型的列上连接两个表,然后用 CSV 连接第二个表吗 - Can I join two tables on an Integer type column and the second with CSV
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM