加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

sql-server – 在update语句中连接多个表

发布时间:2020-12-12 06:48:40 所属栏目:MsSql教程 来源:网络整理
导读:我试图在更新声明中加入三个表,但到目前为止我都没有成功.我知道这个查询适用于连接两个表: update table 1set x = X * Yfrom table 1 as t1 join table 2 as t2 on t1.column1 = t2.column1 但是,在我的情况下,我需要加入三个表,所以: update table 1set x
我试图在更新声明中加入三个表,但到目前为止我都没有成功.我知道这个查询适用于连接两个表:
update table 1
set x = X * Y
from table 1 as t1 join table 2 as t2 on t1.column1 = t2.column1

但是,在我的情况下,我需要加入三个表,所以:

update table 1
set x = X * Y
from table 1 as t1 join table 2 as t2 join table3 as t3 
on t1.column1 = t2.column1 and t2.cloumn2 = t3.column1

不管用.我也尝试了以下查询:

update table 1
set x = X * Y
from table 1,table 2,table 3
where column1 = column2 and column2= column3

有谁知道实现这个的方法?

解决方法

你绝对不想使用表,表,表语法; here’s why.对于您的中间代码示例,连接语法遵循与UPDATE大致相同的SELECT规则. JOIN t2 JOIN t3 ON …无效,但JOIN t2 ON … JOIN t3 ON有效.

所以这是我的建议,尽管应该更新以完全符合y的来源:

UPDATE t1
  SET x = x * y  -- should either be t2.y or t3.y,not just y
  FROM dbo.table1 AS t1
  INNER JOIN table2 AS t2
  ON t1.column1 = t2.column1
  INNER JOIN table3 AS t3
  ON t2.column2 = t3.column1;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读