简体   繁体   English

SQL查询-连接同一列两次

[英]SQL Query - Join the same column twice

I'm having trouble to achieve the result I want trying join a column from a table twice. 我无法获得想要两次尝试从表中连接一列的结果。

My first table is "dbo.Sessions", which contains basic session info like the user ID, the project ID, login/logout date and times, etc. 我的第一个表是“ dbo.Sessions”,其中包含基本会话信息,例如用户ID,项目ID,登录/注销日期和时间等。

I need to join to that the user names and project names. 我需要加入该用户名和项目名。 However, these are found in another table, but in the same column (dbo.tblObjects.Name). 但是,它们在另一个表中但在同一列(dbo.tblObjects.Name)中找到。

Example: 例:

+------+---------------+
| k_Id | Name          |
+------+---------------+
|   1  | AgentName1    |
|   2  | ProjectNameX  |
|   3  | ProjectNameY  |
|   4  | AgentName2    |
|   5  | ProjectNameZ  |
|   6  | AgentName3    |
+------+---------------+

To try and achieve my goal, I used two "LEFT JOIN". 为了实现我的目标,我使用了两个“ LEFT JOIN”。 However, I get duplicate results in both. 但是,我都得到重复的结果。 I'll either get both columns to display either the project names or the user names (depending on which "LEFT JOIN" is first). 我将同时获得两列以显示项目名称或用户名称(取决于“ LEFT JOIN”中的第一个)。

This is what I have at this point: 这是我现在所拥有的:

SELECT SysDB.dbo.Sessions.*, SysDB.dbo.tblObjects.Name AS AgentName, SysDB.dbo.tblObjects.Name AS ProjectName
FROM SysDB.dbo.Sessions
LEFT JOIN SysDB.dbo.tblObjects ON SysDB.dbo.Sessions.userId = SysDB.dbo.Objects.k_Id
LEFT JOIN SysDB.dbo.tblObjects ON SysDB.dbo.Sessions.projectId = SysDB.dbo.Objects.k_Id
WHERE (SysDB.dbo.Sessions.loginDate BETWEEN 'm/d/yyyy' AND 'm/d/yyyy')

Note: SysDB is the name of the database that I identify every time because this query is to be run externally. 注意:SysDB是我每次标识的数据库的名称,因为此查询将在外部运行。 I also don't use "USE SysDB" before my selection because it doesn't work from the VBA macro this will run from. 在选择之前,我也不要使用“ USE SysDB”,因为它不能从将要运行的VBA宏中使用。

Note 2: I have found a thread on this site that addresses this exact issue, but I can't understand what is being done, and it dates back in 2012. Something about aliases. 注意2:我在此站点上找到了一个可以解决此确切问题的线程,但我不知道该怎么做,它可以追溯到2012年。有关别名的内容。 The solution offers to add "ls." 该解决方案可以添加“ ls”。 and "lt." 和“ lt”。 before the table names, but that doesn't work for me. 在表格名称之前,但这对我不起作用。 Says the table doesn't exist. 说表不存在。

SQL Query Join Same Column Twice SQL查询联接同一列两次

Note 3: I have tried many different things, such as: 注意3:我尝试了许多不同的操作,例如:

LEFT JOIN SysDB.dbo.tblObjects AS AgentName ON SysDB.dbo.Sessions.userId = SysDB.dbo.tblObjects.k_Id
LEFT JOIN SysDB.dbo.tblObjects AS ProjectName ON SysDB.dbo.Sessions.projectId = SysDB.dbo.tblObjects.k_Id

Any insights would be greatly appreciated. 任何见解将不胜感激。 Thanks! 谢谢!

You may find it much easier to see what you are doing by giving each table an alias ( session , agent , project below) 通过给每个表一个别名(下面的sessionagentproject ),您可能会发现查看正在做的事情要容易得多。

SELECT session.*, agent.Name AS AgentName, project.Name AS ProjectName
  FROM SysDB.dbo.Sessions session
       LEFT JOIN SysDB.dbo.tblObjects agent 
              ON session.userId = agent.k_Id
       LEFT JOIN SysDB.dbo.tblObjects project 
              ON project.projectId = session.k_Id
 WHERE (session.loginDate BETWEEN 'm/d/yyyy' AND 'm/d/yyyy')

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM