表间的更新语句怎么写?
有两个表:
table1
id field2
001 5
002 7
table2
id field2 field3
001 6 19
002 10 34
003 15 16
现在想实现这样的效果:
table1与table2两个表,关联字段是id,现在判断table1与table2中相同的id值,用table1表中的field2值来更新table2中的field2值,实现如下的效果:
id field2 field3
001 5 19
002 7 34
003 15 16
[最优解释]
EXEC CS'
$table1
id field2
001 5
002 7
$table2
id field2 field3
001 6 19
002 10 34
003 15 16
'
--> 测试数据:[table1]
if object_id('[table1]') is not null drop table [table1]
GO
create table [table1]([id] varchar(3),[field2] int)
insert [table1]
select '001',5 union all
select '002',7
--> 测试数据:[table2]
if object_id('[table2]') is not null drop table [table2]
GO
create table [table2]([id] varchar(3),[field2] int,[field3] int)
insert [table2]
select '001',6,19 union all
select '002',10,34 union all
select '003',15,16
UPDATE t2
SET t2.field2 = t.field2
FROM table1 t INNER JOIN table2 t2 ON t.id=t2.id
SELECT * FROM table2 t
/*
id field2 field3
---- ----------- -----------
001 5 19
002 7 34
003 15 16
(3 行受影响)
*/
drop table [table1]
drop table [table2]
[其他解释]
with tb1(id,field2)
as(
select '001',5 union all
select '002',7
),
tb2(id,field2,field3)
as(
select '001',6,19 union all
select '002',10,34 union all
select '003',15,16
)
select tb1.id,(case when tb1.field2<tb2.field2 then tb1.field2 else tb2.field2 end) field2,field3
from tb1,tb2 where tb1.id=tb2.id
union select * from tb2 where tb2.id not in (select id from tb1)
UPDATE t2
SET t2.field2 = t.field2
FROM table1 t INNER JOIN table2 t2 ON t.id=t2.id
--CREATE TABLE table1( id VARCHAR(10), field2 INT )
--INSERT INTO table1
--SELECT '001', 5
--UNION ALL
--SELECT '002', 7
--CREATE TABLE table2( id VARCHAR(10), field2 INT , field3 INT )
--INSERT INTO table2
--SELECT '001', 6, 19
--UNION ALL
--SELECT '002', 10, 34
--UNION ALL
--SELECT '003', 15 , 16
UPDATE a
SET a.field2=b.field2
FROM table2 a INNER JOIN table1 b ON a.id=b.id
SELECT * FROM table2
/*
id field2 field3
---------- ----------- -----------
001 5 19
002 7 34
003 15 16
(3 行受影响)
*/
--创建表以及初始化数据
IF(OBJECT_ID('table1','U') IS NOT NULL) DROP TABLE table1
CREATE TABLE table1(ID VARCHAR(10) ,field2 INT )
INSERT INTO table1
SELECT '001',5 UNION ALL
SELECT '002',7
IF(OBJECT_ID('table2','U') IS NOT NULL) DROP TABLE table2
CREATE TABLE table2(ID VARCHAR(10) ,field2 INT,field3 INT)
INSERT INTO table2
SELECT '001',6,19 UNION ALL
SELECT '002',10,34 UNION ALL
SELECT '003',15,16
--修改
UPDATE table2
SET field2 =table1.field2
FROM table1,table2
WHERE table1.ID=table2.ID
--结果
SELECT * FROM table2
ID field2 field3
---------- ----------- -----------
001 5 19
002 7 34
003 15 16
(3 行受影响)
update a set a.field2=b.field2 from table2 a inner join table1 b on a.id=b.id
update A set A.col1=B.colm,A.col2=B.coln from B where a.主键=b.主键
if OBJECT_ID('tab1') is not null
drop table tab1
if OBJECT_ID('tab2') is not null
drop table tab2
go
create table tab1(id char(3),field2 int)
create table tab2(id char(3),field2 int,field3 int)
insert into tab1
select '001',5 union all
select '002',7
insert into tab2
select '001',6,19 union all
select '002',10,34 union all
select '003',15,16
select * from tab2
go
update tab2 set field2=a.field2 from tab1 a join tab2
on a.id=tab2.id
select * from tab2