简体   繁体   English

使用第一个表的自动递增的ID更新第二个表

[英]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... 请帮助...提前感谢...

For PHP, this is found with mysql_insert_id() . 对于PHP,可通过mysql_insert_id()找到。 For .NET, take a look at this . 对于.NET,看看这个

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.

相关问题 根据第二个表的 id 对第一个表的结果进行排序 - Sort the results of the first table by the id of the second 插入到第一个时需要更新第二个表 - Need to update a second table upon insert to first 计算第二个表中与第一个表中的ID相匹配的条目 - Count entries from second Table that match id from first Table 当第二个表的 id 依赖于第一个表时,使用 php 一次插入两个表 - Insert two table at a time mysql using php when id of second table is depend on first table 单击提交时如何将第一个表的 id 添加到第二个表的 id_position 字段? - How to add the id of the first table to the id_position field of the second table when I click submit? 计算由id连接的第二个表中的所有行,并将值添加到第一个表的右行 - Count all rows from a second table connected by id and add the value on the right row of the first table 如何从我知道第二张桌子的ID的第一张桌子 - How to get the first table where i know the id from second table 如何更新两个mySQL表并将主键从第一个表输入到第二个表? - How to update two mySQL tables and input primary key from first table into second table? 自动递增的唯一字符串ID - Autoincremented unique string id 如何获取连接表中第一个表的ID - How to get ID of the first table in a join table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM