一个SQL update语句

假如想象 提交于 2020-01-13 16:33:47

须要每隔一段时间选取最老的商户更新时间戳: 

update DP_Shop set DP_Shop.LastDate = now() where DP_Shop.ShopId in (select ShopId from DP_Shop order by LastDate limit 5); 

ERROR 1235 (42000): This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'

多加一层: 

update DP_Shop set DP_Shop.LastDate = now() where DP_Shop.ShopId in (select t.ShopId from (select ShopId from DP_Shop order by LastDate limit 5) as t); 

能够work,但不高效。考虑下怎么优化。

最后是用暂时表:

start transaction;

create temporary table tmp1986 (select ShopId from DP_Shop order by LastDate limit 1000);

update DP_Shop inner join tmp1986 on DP_Shop.ShopId = tmp1986.ShopId set LastDate = now();

DROP TABLE tmp1986; 

commit;

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!