相关表的mysql查询

相关表的mysql查询

问题描述:

我有5张桌子。表1具有主键ID。类似地,其他表也有它们的主键。表1的主键id也是名为faculty_id的其他4个表的外键。现在,表1的id被设置为自动增量。此外,所有表格仅为单个人定义内容(基于表1中的id)。如何在其他faculty_id中插入表1的相同id,外键应自动插入,并且应与主键。但这些外键不是自动增量。因为一个人可以拥有超过1条记录。如何在MySQL中实现这些记录的插入和选择?

I have 5 tables. table 1 has a primary key id. similarly other tables as well have their primary keys. Also the primary key id of table 1 is the foreign key for other 4 tables named as faculty_id.Now this id of table 1 is set as auto increment. Also all the tables will define content only for a single person (based on id in table 1).How to insert the same id of table 1 in the other faculty_id's that is the foreign key should be automatically inserted and should be the same as the primary key.But these foreign keys are not auto increment. Because a single person can have more than 1 records in these. How to achieve insertion and selection of these records in MySQL?

在mysql中,LAST_INSERT_ID()将为您提供上次插入记录的ID。因此,如果您插入表1然后存储LAST_INSERT_ID(),然后在将记录插入相关表时使用它。
In mysql LAST_INSERT_ID() will give you the ID of the last inserted record. So, if you insert into table 1 then store off LAST_INSERT_ID() and then use that as you insert records into the related tables.