使用“选择查询”更新表带有 where 子句
我想实现以下目标:
表(my_table)的当前状态
id totalX totalY totalZ
--------- -------------- -------------- --------------
9 34 334 0
10 6 56 0
11 21 251 0
12 3 93 0
(my_table2)的查询结果
select id,count(*) as total FROM my_table2 WHERE column_2 = 1 GROUP BY id
id total
--------- --------------
9 500
10 600
11 700
12 800
表(my_table)的预期状态
id totalX totalY totalZ
--------- -------------- -------------- --------------
9 34 334 500
10 6 56 600
11 21 251 700
12 3 93 800
可以吗在一个更新查询中完成?我正在 RHEL 5.0 上寻找 Sybase ASE 12.5
编辑: 我找不到 Sybase 的解决方案,但该问题的当前答案适用于 MS SQL Server。
I want to achieve the following:
Current State of table (my_table)
id totalX totalY totalZ
--------- -------------- -------------- --------------
9 34 334 0
10 6 56 0
11 21 251 0
12 3 93 0
Query result of (my_table2)
select id,count(*) as total FROM my_table2 WHERE column_2 = 1 GROUP BY id
id total
--------- --------------
9 500
10 600
11 700
12 800
Expected State of table (my_table)
id totalX totalY totalZ
--------- -------------- -------------- --------------
9 34 334 500
10 6 56 600
11 21 251 700
12 3 93 800
Can this be done in ONE update query ? I am looking for Sybase ASE 12.5 on a RHEL 5.0
EDIT: I coudn't find the solution for Sybase, but the current answer to the question works on MS SQL Server..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
最好、更可靠的方法是使用 MERGE。
参考: http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.infocenter.dc36272.1572/html/commands/commands89.htm
The best and more reliable way is to use MERGE.
Reference : http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.infocenter.dc36272.1572/html/commands/commands89.htm
看起来 Sybase 支持更新连接:
http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.dc20023_1251/html/optimizer/optimizer133.htm
It looks like Sybase supports joins with updates:
http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.dc20023_1251/html/optimizer/optimizer133.htm
像这样的东西应该可以解决问题
something like this should do the trick
更新 在 MS SQL Server 中,这就是您要做的事情。 OP 指出这在 Sybase 中不起作用。
UPDATE In MS SQL Server this is what you would do. The OP noted this doesn't work in Sybase.