我的问题是:
我有2个表:Employees
(它有FirstName
,LastName
和EmployeeId
列)和ProjectWorkers
(它有FullName
和WorkerId
列)。
WorkerId
与EmployeeId
相同,因此我需要使用ProjectWorkers.FullName
和Employees.FirstName
数据的加入数据填充LastName
(根据WorkerId=EmployeeId
})。
我编写了一个填充FullName
列的查询,但只有FirstNames
,但我不明白如何加入FirstName
和FullName
(它有简单但对我来说这是一个问题。)
我的查询:
USE [HRM_MVC_DEV_Test]
GO
UPDATE [dbo].[ProjectWorkers] set
FullName = FirstName
FROM [dbo].[Employees] as e
INNER JOIN [dbo].[ProjectWorkers] as p
ON e.EmployeeId = p.WorkerId
GO
P.S。感谢格式化文本,下次我会尝试自己动手)
答案 0 :(得分:5)
使用+
或CONCAT(从SQL Server 2012开始):
USE [HRM_MVC_DEV_Test]
GO
UPDATE p
SET FullName = FirstName + ' ' + LastName
FROM [dbo].[ProjectWorkers] as p
INNER JOIN [dbo].[Employees] as e
ON e.EmployeeId = p.WorkerId
GO
另一种方式:
SET FullName = CONCAT(FirstName,' ',LastName)
答案 1 :(得分:2)
UPDATE ProjectWorkers SET
ProjectWorkers.FullName = Employees.FirstName FROM
ProjectWorkers INNER JOIN Employees ON
ProjectWorkers.EmployeeId = EmployeesON.EmployeeId