我正在使用MySql,我想实现一个查询。 我有 5 个表,在 MySql 中它们看起来像这样。
Table1- Site:
Site_ID
domain_name
site_name
Table2- Locations:
site_id (Same as from Site)
Table3- Users:
user_id (AI primary key)
site_id
Table4- Users_Roles:
role_id(AI Primary key)
site_id
Table5- Users_Addresss:
user_address_id(AI Primary Key)
user_id (Same as from Users)
site_id
通过一个查询,我想插入到所有这些表中。我的数据库已标准化 我无法想到执行该操作的查询。
我将在 php 文件中使用此查询并使用 ajax 触发它。
最佳答案
首先您需要在 Site 表中插入一条记录
INSERT INTO Site (domain_name,site_name) VALUES ('www.google.com', 'Test site');
然后将最后一个插入 id 即 Site_id 分配给如下变量
SET @site_id = LAST_INSERT_ID(); // This is the Site_id
INSERT INTO Users (site_id) VALUES (@site_id);
现在对所有表执行相同的操作。
谢谢
关于mysql - 插入一张表并使用id插入另一张表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27614491/