简体   繁体   English

SQL:在一个查询中两次选择同一列?

[英]SQL: Select the same column twice in one query?

I'm currently working on a query where I need to pull a list of items, as well as the date they were entered into the system and the name and user ID of the person who entered them. 我目前正在处理一个查询,在该查询中,我需要提取一个项目列表,以及它们输入系统的日期以及输入它们的人员的名称和用户ID。

My Items table contains the columns CreateDate , CreatedBy (this is the person's user ID), LastChanged (this is a date), and LastChangedBy (also a user ID, being as the person who entered the item isn't always the same person who created it). 我的Items表包含列CreateDateCreatedBy (这是该用户的用户ID), LastChanged (这是一个日期)和LastChangedBy (也是用户ID),因为输入该项目的人并不总是创建了)。

I have the Items table joined with a User table so I can pull the first and last name of the users who match the CreatedBy and LastChangedBy user IDs. 我将Items表与User表结合在一起,因此可以提取与CreatedByLastChangedBy用户ID匹配的用户的CreatedByLastChangedBy The way I have the query written now, I've concatenated the two fields so they end up displayed as " LastName, FirstName " in a single column. 我现在编写查询的方式是,将两个字段串联起来,使它们最终在单列中显示为“ LastName,FirstName ”。 I also want to only pull results for a certain user, and this user just needs to be the last person who changed the item - it doesn't matter whether or not they created it. 我也只想为某个用户获取结果,而该用户只需是更改项目的最后一个人-他们是否创建项目都无所谓。

My query looks something like this: 我的查询看起来像这样:

SELECT 
    i.ItemName, i.CreateDate, 
    (u.LastName + ', ' + u.FirstName) as [Created By Name], 
    i.CreatedBy, i.LastChanged, 
    (u.LastName + ', ' + u.FirstName) as [Last Changed By Name],
    i.LastChangedBy
FROM 
    Items i 
JOIN 
    Users u ON i.CreatedBy = u.UserID
WHERE 
    i.LastChangedBy = 0001;

My issue though is that since I have the exact same piece of code to pull the user's name both times, it's pulling the same name regardless of whether the user IDs are different in the CreatedBy and LastChangedBy columns. 但是,我的问题是,由于我有完全相同的代码来两次拉用户名,因此无论用户ID在CreatedByLastChangedBy列中是否不同,它都拉相同的名称。 So I end up with results that look like this: 因此,我最终得到的结果如下所示:

ItemName     CreateDate     Created By Name    CreatedBy    LastChanged    Last Changed By Name    LastChangedBy

Item A       12/01/2015     Smith, John        0001         12/03/2015     Smith, John             0001
Item B       12/02/2015     Smith, John        0001         12/04/2015     Smith, John             0001
Item C       12/02/2015     Doe, Jane          1002         12/05/2015     Doe, Jane               0001

So even though John Smith was the last person to change Item C, it's still displaying Jane Doe's name there because (I assume) the strings of code to display the user's name is the same in both instances and it's just pulling the same name both times. 因此,即使约翰·史密斯(John Smith)是最后一个更改项目C的人,它仍然在那里显示Jane Doe的名字,因为(我假设)用于显示用户名的代码字符串在两种情况下都是相同的,并且两次都只是拉相同的名字。

Is there a way to pull the first and last names from the User table twice in the same query but make sure they correspond with the correct user IDs? 有没有一种方法可以在同一查询中两次从User表中提取名字和姓氏,但要确保它们与正确的用户ID对应?

I think you are missing a join in your query: 我认为你缺少一个join在您的查询:

SELECT i.ItemName, i.CreateDate,
       (uc.LastName + ', ' + uc.FirstName) as [Created By Name], 
       i.CreatedBy, i.LastChanged,
       (ucb.LastName + ', ' + ucb.FirstName) as [Last Changed By Name],
       i.LastChangedBy
FROM Items i LEFT JOIN
     Users uc
     ON i.CreatedBy = uc.UserID LEFT JOIN
     Users ucb
     ON i.LastChangedBy = ucb.UserId
WHERE i.LastChangedBy = 0001;

You need to join twice to get both users. 您需要加入两次才能获得两个用户。

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

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