【问题标题】:I want to update one table data based on the join condition with another table in SQL server我想根据 SQL Server 中另一个表的连接条件更新一个表数据
【发布时间】:2019-03-01 05:12:01
【问题描述】:

我尝试使用连接条件从另一个表更新一个表中的列。我尝试了所有可能的方法,但它显示的 SQL 命令没有正确结束。我正在 sql server 中尝试它。

update a 
   set a.col4=b.col4 
from dummy_jd a 
  join --select * from dummy_jd a, 
     (select col1,col2,col3,col4,col5,sum(reported_amount) reported_amount 
      from dummy_jd_2 
      where col5=3843 
      and col3='abc' 
      and col1=4 
      and col2=3002 
      group by col1,col2,col3,col4,col5
  ) b on (a.col2=b.col5 and a.col4=b.col3) 
where a.col1=9;

这是我试过的

【问题讨论】:

  • update a set a.col4=b.col4 from dummy_jd a join --select * from dummy_jd a, (select col1,col2,col3,col4,col5,sum(reported_amount)reported_amount from dummy_jd_2 where col5=3843 and col3='abc' and col1=4 and col2=3002 group by col1,col2,col3,col4,col5)b on (a.col2=b.col5 and a.col4=b.col3) where a .col1=9;这是我试过的......
  • 您使用的是什么版本的 SQL(例如 MySQL、Oracle、SQL Server)? “SQL”只是一种语言,不是实际产品。
  • 亲爱的蒂姆,我不太了解这可能是 sql server 或 oracle
  • 你需要找出这个,否则这个网站上没有人能帮助你。
  • 你能帮我怎么找到

标签: sql-server join sql-update


【解决方案1】:

试试下面 -

update a set a.col4=b.col4 from dummy_jd a
join 
(select col1,col2,col3,col4,col5,sum(reported_amount) reported_amount 
from dummy_jd_2 where col5=3843 and col3='abc' and col1=4 and col2=3002 
group by col1,col2,col3,col4,col5
)b on a.col2=b.col5 and a.col4=b.col3
where a.col1=9

【讨论】:

  • 谢谢...我尝试了完全相同但仍然抛出“sql命令未正确结束错误”
【解决方案2】:
    Thanks to everyone i achieved it by using for loopand joins  below is the query i used
    for i in (select * from dummy_jd1)
    loop 
    update dummy_jd a set a.col4 = (select distinct col4 from dummy_jd_2  b
                     where /*(jnl_ref_no, bill_ref_no) in (select 
                       jnl_ref_no,bill_ref_no from dummy_jd1) and*/
                        a.col2 = b.col5
                       and a.col4 = b.col3
                       and b.col5 = i.col2
                       and b.col3 = i.col3
                       and id_type = 4
                          --and id_value in(3002,3019)
                       and reported_amount <> 0
                     group by col1,col2,col3,col4,col5)


  where a.id_type = 9
  and a.col2=i.col2
  and a.col4=i.col3;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-19
    • 1970-01-01
    • 1970-01-01
    • 2020-12-24
    相关资源
    最近更新 更多