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 行)
...全文
188 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
lxd99423 2004-08-31
  • 打赏
  • 举报
回复
不对,它不会从前往后找,不信你试试
heijiangff 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取的是什么?
【有功-无功协调优化】基于改进多目标粒子群优化算法(小生境粒子群算法)的配电网有功-无功协调优化研究(Matlab代码实现)内容概要:本文围绕配电网的有功-无功协调优化问题展开研究,提出了一种基于改进多目标粒子群优化算法(小生境粒子群算法)的解决方案,并通过Matlab代码实现仿真验证。研究旨在通过优化算法有效降低网络损耗、提升电压稳定性并提高配电系统运行效率,尤其适用于含有分布式能源接入的现代配电网。文中详细阐述了算法改进策略、目标函数构建、约束条件处理及仿真结果分析,展示了该方法相较于传统算法在收敛性和多样性方面的优势。; 适合人群:具备电力系统基础知识和Matlab编程能力的研究生、科研人员及从事智能电网优化工作的工程师。; 使用场景及目标:①应用于含高比例可再生能源接入的配电网优化运行;②用于教学与科研中多目标优化算法的对比研究与改进;③为电力系统调度、无功补偿配置等实际工程问题提供算法支持与仿真验证手段。; 阅读建议:建议读者结合Matlab代码深入理解算法实现细节,重点关注小生境机制在维持种群多样性中的作用,并可通过修改目标函数或引入更多约束条件进行扩展研究,以提升解决复杂工程问题的能力。

34,872

社区成员

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

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