MYSQL根据表A中的列值将记录从表A插入表B和C(通过外键链接)
一直在寻找解决 mysql 插入问题的简单方法。问题如下:
我正在整理一个由部门和办公桌组成的组织数据库。一个部门可能有也可能没有n张办公桌。
部门和服务台都有自己的表,通过服务台中的外键链接到部门中的相关记录(即 pk)。我有一个临时表,用于放置所有新的部门数据(n 条记录长)...在此表中,一个部门的 n 条办公桌记录紧随其后的部门记录。在TEMP表中,如果列department_name有值,则它是一个部门,如果没有,它将有列desk的值,因此将是与上述部门相关的办公桌。正如我所说,在您找到下一个部门记录之前,可能会有几份桌面记录。
这是表记录的示例
TEMP{Department,Desk main_telephone, telephone2, telephone3, email, email2, website}
departments(department, telephone1, telephone2, telephone3, email, email2, website)
desks(Desk, foreignkey=Department id, telephone1, telephone2, telephone3, email, email2, website)
好的,所以我想做的是以下内容:
从 TEMP 中,将部门插入到部门表中,将其办公桌插入办公桌表中,在办公桌记录中生成外键相关部门id。
这是我目前所拥有的
INSERT INTO departments(department,main_telephone,telephone2,telephone3,main_fax,website,email,email2)
SELECT Department,Tel1, Tel2, Tel3, Fax, Email, Email2, Web
FROM temp WHERE Department != '';
SELECT @last_department := LAST_INSERT_ID();
INSERT INTO department_desks(department_id,department, telephone, extension, telephone2, extension2, telephone3, extension3, fax, email)
SELECT @last_department, Desk,Tel1, Ext1, Tel2, Ext2, Tel3, Ext3,Fax, Email
FROM temp WHERE Desk != '';
。除了last_insert_id返回上面INSERT INTO语句中生成的第一个id之外,这是有效的,因此所有办公桌都有相同的department_id,我需要相对于部门...
在伪代码中:
for each record in TEMP table
if Department
INSERT the record into Departments
get the id of the newly created Department record and store it somewhere
else if Desk
INSERT the desk into the desks table with the relevant departments id as the foreignkey
再次注意,所有部门办公桌都直接遵循 TEMP 表中的部门
非常感谢
Have been searching high and low for a simple solution to a mysql insert problem. The problem is as follows:
I am putting together an organisational database consisting of departments and desks. A department may or may not have n number of desks.
Both departments and desks have their own table linked by a foreign key in desks to the relevant record in departments (i.e. the pk). I have a temporary table which I use to place all new department data (n records long)...In this table n number of desk records for a department follow the department record directly below. In the TEMP table, if a column department_name has a value,it is a department, if it doesn't it will have a value for the column desk and therefore will be a desk which is related to the above department. As I said there maybe several desk records until you get to the next department record.
Here is a sample of the table records
TEMP{Department,Desk main_telephone, telephone2, telephone3, email, email2, website}
departments(department, telephone1, telephone2, telephone3, email, email2, website)
desks(Desk, foreignkey=Department id, telephone1, telephone2, telephone3, email, email2, website)
Ok, so what I want to do is the following:
From TEMP, Insert the departments into the departments table and their desks into the desks table , generating a foreign key in the desk record to the relevant departments id.
Here is what I have at the moment
INSERT INTO departments(department,main_telephone,telephone2,telephone3,main_fax,website,email,email2)
SELECT Department,Tel1, Tel2, Tel3, Fax, Email, Email2, Web
FROM temp WHERE Department != '';
SELECT @last_department := LAST_INSERT_ID();
INSERT INTO department_desks(department_id,department, telephone, extension, telephone2, extension2, telephone3, extension3, fax, email)
SELECT @last_department, Desk,Tel1, Ext1, Tel2, Ext2, Tel3, Ext3,Fax, Email
FROM temp WHERE Desk != '';
This works except that the last_insert_id returns the first id generated in the above INSERT INTO statment,therefore all desk have the same department_id and I need the id relative to the department...
In pseudo-ish code:
for each record in TEMP table
if Department
INSERT the record into Departments
get the id of the newly created Department record and store it somewhere
else if Desk
INSERT the desk into the desks table with the relevant departments id as the foreignkey
note once again that all departments desks directly follow the department in the TEMP Table
Many Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
表 A { id, dept, desk }
表 B { id,foreignkey, dept }
insert into B (id,foreignkey, dept) select null, id, dept from A;
Table A { id, dept, desk }
Table B { id, foreignkey, dept }
insert into B (id, foreignkey, dept) select null, id, dept from A;
好的,找到解决方案了。我将必须创建一个存储过程并使用游标。我将报告我的进展。
Ok, found the solution. I am going to have to create a stored procedure and use a cursor. I shall report back on my progress.