使用“选择查询”更新表带有 where 子句

发布于 2024-09-24 15:23:56 字数 1385 浏览 1 评论 0原文

我想实现以下目标:

表(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

莫言歌 2024-10-01 15:24:05

最好、更可靠的方法是使用 MERGE。

MERGE INTO my_table
USING (select id,count(*) as total FROM my_table2 WHERE column_2 = 1 GROUP BY id) t2
ON (my_table.id=t2.id)
WHEN MATCHED THEN
UPDATE SET totalZ = t2.total

参考: 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.

MERGE INTO my_table
USING (select id,count(*) as total FROM my_table2 WHERE column_2 = 1 GROUP BY id) t2
ON (my_table.id=t2.id)
WHEN MATCHED THEN
UPDATE SET totalZ = t2.total

Reference : http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.infocenter.dc36272.1572/html/commands/commands89.htm

你是暖光i 2024-10-01 15:24:03

像这样的东西应该可以解决问题

update my_table
set my_table.totalZ = (select count(*) from my_table2 where my_table.id = my_table2.id and my_table2.column_2 = 1);

something like this should do the trick

update my_table
set my_table.totalZ = (select count(*) from my_table2 where my_table.id = my_table2.id and my_table2.column_2 = 1);
沐歌 2024-10-01 15:24:02
   update 
          my_table 
   set 
      my_table.totalZ = t.total 
   FROM
    my_table mt
    INNER JOIN 
       (select id,count(*) as total 
       FROM my_table2 
      WHERE column_2 = 1 GROUP BY id) t
   on mt.id  = t.id

更新 在 MS SQL Server 中,这就是您要做的事情。 OP 指出这在 Sybase 中不起作用。

   update 
          my_table 
   set 
      my_table.totalZ = t.total 
   FROM
    my_table mt
    INNER JOIN 
       (select id,count(*) as total 
       FROM my_table2 
      WHERE column_2 = 1 GROUP BY id) t
   on mt.id  = t.id

UPDATE In MS SQL Server this is what you would do. The OP noted this doesn't work in Sybase.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文