update问题,我快要晕了!

lxd99423 2004-08-31 11:03:58
create table ttt(
id int,
id1 int
)

create table ttt1(
id int,
id1 int
)
insert ttt1
select 1,0

insert ttt
select 1,1
union
select 1,2
union
select 1,3

update a set id1=a.id1+b.id1 from ttt1 a,ttt b where a.id=b.id

select * from ttt1
-----------
id id1 name
----------- ----------- --------
1 3 1

(所影响的行数为 1 行)
drop table ttt
drop table ttt1

--我想得到这种结果,用update怎么写啊?
id id1 name
----------- ----------- --------
1 6 1

(所影响的行数为 1 行)
...全文
177 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
lxd99423 2004-08-31
  • 打赏
  • 举报
回复
不对,它不会从前往后找,不信你试试
myf7961 2004-08-31
  • 打赏
  • 举报
回复
是啊,你现在才知道?他从前面往后找,如果找到就替换先前的!!所以是最后的一条,知道了????
lxd99423 2004-08-31
  • 打赏
  • 举报
回复
对不对啊,没人理我
lxd99423 2004-08-31
  • 打赏
  • 举报
回复
我明白了,在更新的时候,根本不会一条一条的找,而是从后往前找第一条

记得以前FoxBase就是这样处理的
LoveSQL 2004-08-31
  • 打赏
  • 举报
回复
create table ttt(
id int,
id1 int
)

create table ttt1(
id int,
id1 int
)
insert ttt1
select 1,0

insert ttt
select 1,1
union
select 1,2
union
select 1,3

--测试
update a set a.id1=b.id1 from ttt1 a,(select id, sum(id1) as id1 from ttt group by id) b where a.id=b.id

select * from ttt1
drop table ttt,ttt1

--结果
id id1
----------- -----------
1 6

(1 row(s) affected)
老宛 2004-08-31
  • 打赏
  • 举报
回复
从你这个例子来看,是这样的
lxd99423 2004-08-31
  • 打赏
  • 举报
回复
这个结论对吗:update更新时,如果有多条记录匹配,以最后一条为准
lxd99423 2004-08-31
  • 打赏
  • 举报
回复
不好意思,我试的时候加了这个字段,没用的,看id1的值就行了
老宛 2004-08-31
  • 打赏
  • 举报
回复
--不好意思,我3楼写错了
create table ttt(
id int,
id1 int
)

create table ttt1(
id int,
id1 int
)
insert ttt1
select 1,0

insert ttt
select 1,1
union
select 1,2
union
select 1,3

update ttt1 set id1=id1+(select sum(id1) from ttt where id=ttt1.id)


select * from ttt1

drop table ttt
drop table ttt1
notin 2004-08-31
  • 打赏
  • 举报
回复
update a set a.id1=b.id1 from ttt1 a inner join (select id,sum(id1) as id1 from ttt group by id) b on a.id=b.id

name 字段不知道你是怎么取的
good2speed 2004-08-31
  • 打赏
  • 举报
回复
UPDATE ttt1 SET id1 = b.id1
FROM
ttt1 AS a,
(
SELECT a.id,SUM(b.id1) AS id1
from ttt1 a,ttt b where a.id=b.id
GROUP BY a.id
) AS b
WHERE a.id=b.id
老宛 2004-08-31
  • 打赏
  • 举报
回复
create table ttt(
id int,
id1 int
)

create table ttt1(
id int,
id1 int
)
insert ttt1
select 1,0

insert ttt
select 1,1
union
select 1,2
union
select 1,3

--update a set a.id1=a.id1+b.id1 from ttt1 a,ttt b where a.id=b.id
--update a set a.id1=a.id1+b.id1 from ttt1 a,(select id,sum(id1) as id1 from ttt group by id) b where a.id=b.id
update ttt1 set id1=(select sum(id1) from ttt where id=ttt1.id)


select * from ttt1

drop table ttt
drop table ttt1
Limperator 2004-08-31
  • 打赏
  • 举报
回复
update a set a.id1 = a.id1 + b.id1 from ttt1 a, (select id, id1 = sum(id1) from ttt group by id) b where a.id = b.id
Limperator 2004-08-31
  • 打赏
  • 举报
回复
update a set a.id1 = a.id1 + b.id1 from ttt1 a, (select id, id1 = sum(id1) from ttt group by id) b where a.id = b.id
老宛 2004-08-31
  • 打赏
  • 举报
回复
create table ttt(
id int,
id1 int
)

create table ttt1(
id int,
id1 int
)
insert ttt1
select 1,0

insert ttt
select 1,1
union
select 1,2
union
select 1,3

--update a set a.id1=a.id1+b.id1 from ttt1 a,ttt b where a.id=b.id
update a set a.id1=a.id1+b.id1 from ttt1 a,(select id,sum(id1) as id1 from ttt group by id) b where a.id=b.id

select * from ttt1

drop table ttt
drop table ttt1
zhangzs8896 2004-08-31
  • 打赏
  • 举报
回复
你更新哪个表?ttt?那你结果中的name取的是什么?

34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧