【问题标题】:sql join tables based on 2 parameters and update 2 columnssql连接表基于2个参数并更新2列
【发布时间】:2016-01-23 17:05:41
【问题描述】:

当我想基于 1 个参数加入 2 个表并只更新 1 列时,我使用以下语法

UPDATE your_table
INNER JOIN your_temp_table on your_temp_table.name= your_table.name
SET your_table.surname= your_temp_table.surname;

但是,如果我想基于 2 个参数(姓名和城市)连接表并更新 2 列(姓氏和日期),该怎么办。你能帮我找到解决办法吗?

【问题讨论】:

    标签: mysql sql


    【解决方案1】:

    使用逗号设置多列:

    UPDATE your_table
    INNER JOIN your_temp_table on your_temp_table.name= your_table.name and 
                your_temp_table.city= your_table.city
    SET your_table.surname= your_temp_table.surname,
        your_table.`date`=your_temp_table.`date`;
    

    【讨论】:

      【解决方案2】:

      from 之后为表和连接语句使用别名。

      update yt 
      set yt.name='name_value', yt.date= 'date_value' 
      from your_table yt
      inner join your_temp_table ytt on ytt.surname= yt.surname 
      and ytt.City = yt.City
      

      【讨论】:

        猜你喜欢
        • 2019-01-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-19
        • 2011-03-10
        • 1970-01-01
        • 2017-12-15
        • 1970-01-01
        相关资源
        最近更新 更多