mysql 存储过程如何实现类似的功能

qq1303710309 2017-11-15 02:16:16
除了使用group_concat,还有其他方法可以实现类似的代码吗?

set ids = (select GROUP_CONCAT(`id`) from `table` where flag=0);
insert into `table` select * from table where id in(ids);
update `table` set flag=1 where id in(ids);
...全文
195 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjcxc 2017-11-23
  • 打赏
  • 举报
回复
放在事务就可以了 start transaction; insrt .... update ... commit;
zjcxc 2017-11-15
  • 打赏
  • 举报
回复
=sql]insert into `table` select * from table where id in(select id from `table` where flag=0); update `table` set flag=1 where id in(select id from `table` where flag=0[code[/code]);
qq1303710309 2017-11-15
  • 打赏
  • 举报
回复
引用 1 楼 zjcxc 的回复:
=sql]insert into `table` select *,-1 from table where id in(select id from `table` where flag=0); update `table` set flag=1 where id in(select id from `table` where flag=0[code[/code]);
这个存储过程有可能并发调用,这样的话应该会出现重复insert的情况。 for update应该也不行,select ... for update 到update执行时间,如果有新的记录,这些新记录没有insert就设flag=1了。

56,678

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧