从另一个表中的MAX日期更新日期字段

时间:2012-02-23 07:37:53

标签: mysql

我正在尝试根据另一个表中的最新日期更新一个表中的DATETIME字段。两个表之间的公共字段名为msisdn。我正在尝试这个

update table1 t1
join table2 t2
on t1.msisdn = t2.msisdn
set t1.bill_attempt = (Select max(event_time) from table2
where t1.msisdn = t2.msisdn)

在table1中,我获得了所有msisdn记录的最新相同事件时间。请协助

2 个答案:

答案 0 :(得分:4)

或者:

UPDATE table1 t1
SET bill_attempt = 
      ( SELECT MAX(event_time) 
        FROM table2 t2
        WHERE t1.msisdn = t2.msisdn
      )

或:

UPDATE
      table1 t1
  JOIN 
      ( SELECT msisdn
             , MAX(event_time) AS event_time
        FROM table2 
        GROUP BY msisdn
      ) AS t2
  ON t1.msisdn = t2.msisdn
SET t1.bill_attempt = t2.event_time 

答案 1 :(得分:0)

UPDATE table1 t1
JOIN (SELECT MAX(event_time) AS event_time,msisdn FROM table2 GROUP BY msisdn) t2
ON t1.msisdn = t2.msisdn
SET t1.bill_attempt = t2.event_time