我有一个事务日志表,每次更新记录时都会更新。更新涉及groupby子句的表,同时避免临时表
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;
艾杰瑞尔的解决方案工作!
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
;
加入指数ID +日期应该帮助, 不知道表的大小和u似乎SELECT *无极限......是一种效率不高,UPDATE语句是错误的原因在GROUP BY中缺少表别名 – ajreal 2010-11-09 21:00:08
我正在处理的这个表返回700行,因此没有限制。所以一个表别名应该使更新查询工作组?你能否提出一个示例查询?我通常不用复杂的查询,但临时表是我讨厌的东西! – ThinkCode 2010-11-09 21:04:33