现在的位置: 首页 > 数据库 > 正文

MYSQL使用Limit限定更新行数

2020年05月02日 数据库 ⁄ 共 567字 ⁄ 字号 评论关闭

想要修改config表,将其中5607行的is_ok改为true。
  想通过下面的sql语句实现
  Sql代码
  UPDATE channel_config set is_adam_pub=1 where channel_id in (select channel_id from channel_config limit 5607);
  发现Mysql不能支持子句使用Limit,数据库会报错
  This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
  网上找一方法,可以支持子串使用Limit
  Sql代码
  UPDATE channel_config set is_adam_pub=1 where channel_id in (select t.channel_id from (select channel_id from channel_config limit 5607)as t);
  这样处理虽然能达到效果,但是执行很慢,用了15.815ms
  转念一想,update是不是也有limit用法
  Sql代码
  UPDATE channel_config set is_adam_pub=1 LIMIT 5607;
  发现竟然成功了,而且就用了0.102ms
  --end--

抱歉!评论已关闭.