[英]update second table with the autoincremented id of the first table
This is my first post on the site. 这是我在网站上的第一篇文章。 I'm a newbie with mysql and php so please pardon any mistakes and kindly point them out. 我是mysql和php的新手,所以请原谅任何错误并指出错误。 I have a table to store hiring details: 我有一张桌子来存储招聘信息:
table_hiring table_hiring
hiringid int(20) AUTOINCREMENT => (primary key); hiringid int(20)AUTOINCREMENT =>(主键);
hiringname varchar(80); hiringname varchar(80);
hiringdate bigint(20); hiringdate bigint(20);
... ... ……
I have another table to store the tools and their hire prices which has: 我还有另一个表来存储工具及其租金,其中包括:
table_tools table_tools
tool_id int(10)AUTOINCREMENT => (primary key); tool_id int(10)AUTOINCREMENT =>(主键);
tool_description varchar(100); tool_description varchar(100);
tool_price double; tool_price double;
Each hire request would be saved in the hiring table. 每个雇用请求将保存在雇用表中。 My problem is that each hire request can have multiple tools(Its a ground hire. so the hirer can request a hurdles set, a shotput set and a javelin set and so on.). 我的问题是,每个租用请求都可以有多个工具(它是地面租用。因此,租用者可以请求障碍组,铅球组和标枪组等)。 I was stumped with the problem of storing multiple values in a single field. 我为在单个字段中存储多个值而感到困惑。 I did some research and decided to have another table 我做了一些研究,决定再要一张桌子
table_tool_hire table_tool_hire
tool_hire_id() => (primary key from table_hiring); tool_hire_id()=>(来自table_hiring的主键);
tool_id() => (primary key from table_tools); tool_id()=>(来自table_tools的主键);
tool_hire_date bigint(20); tool_hire_date bigint(20);
The problem is that everytime there is a hire request, according to the hirer's request i need to populate table_hiring and table_tool_hire in one query and the hire Id needs to be the matching in both the tables. 问题是,每当有一个租用请求时,根据租用者的请求,我需要在一个查询中填充table_hiring和table_tool_hire,并且租用ID必须在两个表中都匹配。
How do I insert the values into table_hiring and simultaneously get the autoincremented id value of that hire to be updated into the table_tool_hire table?? 如何将值插入到table_hiring中,同时获取要更新到table_tool_hire表中的该员工的自动递增的ID值? Please help...Thanks in advance... 请帮助...提前感谢...
please take a look to this forum, 请看看这个论坛,
Get all insert ids in a transaction 获取交易中的所有插入ID
you can use variables an store there the last insert id, 您可以在最后一个插入ID的存储区中使用变量,
INSERT INTO messages(`message`) VALUES ('message1');
@message1:=last_insert_id();
INSERT INTO messages_tags(`message_id`, `tags`) VALUES (LAST_INSERT_ID(), 'tagfoo1');
@message2:=last_insert_id();
if you make 如果你做
select @message1;
the result would be 1234, so you have 结果是1234,所以你有
@message1 => 1234
@message2 => 1235
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.