【问题标题】:SQL update 1 table from 2 tablesSQL 从 2 个表更新 1 个表
【发布时间】:2016-09-30 09:10:07
【问题描述】:

我需要一些帮助来创建更新查询。例如我有

select:(T1- table 1, T2- tables 2, Z1 -column from table 1, Z2 Column from table 2 etc)

Select * from T1,T2 where X1=X2 and Y1=Y2 and Z1=Z2 and Datea>=20160601 and Dateb<20160720 and B1=2 and C1=5 and D1=10 and E2 in (A+,A-, 22,33,44)

现在我想写一个使用参数的update

from where
update T1 set F1, G1 where

正确的条件怎么写?

【问题讨论】:

  • 您可以在更新中执行子查询吗?
  • 例如..UPDATE TABLENAME SET F1 = (SELECT * FROM)
  • 对不起,我写错了。更新应该像 update T1 set F1=10, G1=50 where
  • 您仍然可以在每个 SET 语句中使用子查询来获得该值吗?
  • 解决方案可能是更新 T1 设置 F1=10, G1=50 where exists (Select * from T1,T2 where X1=X2 and Y1=Y2 and Z1=Z2 and Datea>=20160601 and Dateb

标签: mysql sql logic


【解决方案1】:
update T1 set F1=10, G1=50 
where exist (Select F1, G1 
             from T1,T2 where X1=X2 
             and Y1=Y2 and Z1=Z2 
             and Datea>=20160601 
             and Dateb<20160720 
             and B1=2 
             and C1=5 
             and D1=10 
             and E2 in (A+,A-, 22,33,44))

你必须选择需要在 select 子句中更新的列。

也尝试先运行select语句,看看是否得到了你想要的结果

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-30
    • 1970-01-01
    • 1970-01-01
    • 2012-03-31
    • 2013-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多