我有一个事务日志表,每次更新记录时都会更新。
autoid ID Date Source
1 1 2010-10-11 abc
2 2 2010-09-10 xyz
3 1 2010-08-03 pqr
4 1 2010-11-01 mno
我可以通过以下查询获取每个ID的最新更新(它有效,是吗?):
select * from mytable group by ID order by Date desc;
现在,如果我有另一个应该使用最新事务日期更新的表,那么如何在不创建临时表的情况下进行此操作?
以下查询不正确,但是是否有嵌套查询替代?我不想创建临时表。
update mytable a, othertable b
set b.date = a.date
where b.ID = a.ID group by ID order by Date desc;
ajreal的解决方案有效!
update othertable,
(select b.id, max(b.`date`) as latest from mytable b group by b.id) as b
set othertable.`date` = b.latest
where othertable.id=b.id
;
答案 0 :(得分:1)
update mytable,
(select b.id, max(b.`date`) as latest from othertable b group by b.id) as b
set mytable.`date`=b.latest
where mytable.id=b.id
;