表关联使用INNER JOIN实现更新功能
程序员文章站
2022-04-16 09:25:46
准备一些数据,创建2张表,表1为学生表: CREATE TABLE [dbo].[Student] ( [SNO] INT NOT NULL PRIMARY KEY, [Name] NVARCHAR(10) NOT NULL, [Performance] DECIMAL(18,2) NULL ) G ......
准备一些数据,创建2张表,表1为学生表:
create table [dbo].[student] ( [sno] int not null primary key, [name] nvarchar(10) not null, [performance] decimal(18,2) null ) go
表2为学生成绩表:
接下来,我们为上面2张表添加一些数据,并查询数据结果:
insert into [dbo].[student]([sno],[name]) values (10001,n'甲'), (10002,n'乙'), (10003,n'丙'), (10004,n'丁'), (10005,n'戊'), (10006,n'已'), (10007,n'庚') go insert into [dbo].[performance]([sno],[score]) values (10002,320), (10004,380), (10001,424), (10007,467), (10005,300) go select * from [dbo].[student] go select * from [dbo].[performance] go
以上均是为本篇所准备的数据。
把表2的字段[score]值更新至表1的[performance]。
update s set s.[performance] = p.[score] from [dbo].[student] as s inner join [dbo].[performance] as p on(s.[sno] = p.[sno]) go select * from [dbo].[student] go
上一篇: 数据分析与挖掘