【问题标题】:Update table where value from another table is equals to another table value更新表,其中另一个表的值等于另一个表的值
【发布时间】:2016-11-09 22:48:42
【问题描述】:

我想根据表 3 和表 2 的值更新表 1

表1

表2

表3

我想更新 table1 的列 'price',其中 table3 中 table1 的 'store_num' 的 store_div 的值等于 table2 中的 store_div。

我已经尝试加入 table1 和 table3 以获取每个 store_num 的 store_div 值,但我不知道将 where 子句指向哪里,因为 table1 上不存在 store_div。

更新将如下所示:

Update Table1 t1 set price = '0'
join Table t3 on t1.store_num = t3.store_num
where ([t1.store_num]'s [store_div] = Table2.store_div)

我不太确定 where 子句。

任何帮助将不胜感激。

【问题讨论】:

  • @John F,您是否尝试将每个商店的价格更新为“0”,该商店位于特定部门中?我只是想更清楚地了解您要做什么。
  • 是的,其中商店的部门编号出现在 table2 上
  • 嗨@ChrisPickford 我不认为它是重复的,因为我正在尝试更新一个表,其中 where 子句中的值不在将要更新的表中。

标签: sql sql-server


【解决方案1】:

这应该可以在 SQL Server 中使用。

Update Table1 
set price = '0'
From Table3 t3 
inner join Table1 t1
  on t1.store_num = t3.store_num
inner join Table2 t2
  on t2.Store_Div = t3.Store_Div 

您的联接已关闭,但您需要包含 Table2 以确保仅更新在 Table2 中登录 Division 的商店。

【讨论】:

    猜你喜欢
    • 2020-03-18
    • 2012-08-02
    • 1970-01-01
    • 2016-12-01
    • 2023-03-06
    • 2021-04-06
    • 1970-01-01
    • 2010-10-21
    相关资源
    最近更新 更多