简体   繁体   English

同时提交多个查询mysql两次插入相同的id

[英]Submit multiple queries same time mysql inserting same id twice

Want to submit auto-increment value of table_a into table_b at same time. 想要同时将table_a自动递增值提交到table_b中。 I first inserting record into table_a and then fetching last primary id from table_a and inserting it into table_b . 我首先将记录插入table_a ,然后从table_a获取最后一个主要id并将其插入table_b It works well at slow speed like 20 records per sec, but a fast speed and multi user level it inserting duplicate id of table_a into table_b . 它在低速下运行良好,如每秒20条记录,但速度快,多用户级别,它将table_a重复id插入table_b

在此输入图像描述

Is my approach is wrong ? 我的做法是错的吗? Please suggest better way to do this. 请建议更好的方法来做到这一点。

code

query1 = "insert into `table_a` (`aid`,`name`) values(null,'val')";

query2 = "select `id` from `table_a` order by `id` desc limit 1";

$aid='retrieved_value';

query3 = "insert into table_b (`bid`,`aid`,`btype`) values (null,'$aid','val')";

Yes, your approach is wrong. 是的,你的做法是错误的。 It is very racy. 这非常有趣。 You have no guarantee that query2 returns the id related to query1 . 您无法保证query2返回与query1相关的id。 A better approach is to use the last_insert_id function. 更好的方法是使用last_insert_id函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM