[英]SQL Statement to INSERT into one table from two (using id from another w/no join criteria)
I have two tables: 我有两个表:
CREATE TABLE `data_items` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`data` longtext NOT NULL,
`created` datetime NOT NULL,
`updated` datetime NOT NULL,
`data_item_type` varchar(255) NOT NULL,
`data_source_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `IDX_AFA125D21A935C57` (`data_source_id`),
CONSTRAINT `FK_AFA125D21A935C57` FOREIGN KEY (`data_source_id`) REFERENCES `data_sources` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7192 DEFAULT CHARSET=utf8;
AND 和
CREATE TABLE `map_locations` (
`id` int(11) NOT NULL,
`latitude` decimal(9,6) DEFAULT NULL,
`longitude` decimal(9,6) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
`address` varchar(255) DEFAULT NULL,
`status` varchar(255) NOT NULL,
`message_codes` longtext NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `FK_B28E0DE7BF396750` FOREIGN KEY (`id`) REFERENCES `data_items` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I'm trying to accomplish a two-step process. 我正在尝试完成一个两步过程。 The first query works fine:
第一个查询工作正常:
INSERT INTO data_items (`data`, created, updated, data_item_type, data_source_id) SELECT data_items.data, now(), now(), data_items.data_item_type, 2
FROM data_items
WHERE data_items.data_source_id = 1
--> 1 and 2 are example values -> 1和2是示例值
Now, I want to take every id from 'data_items' (the auto-incs that just got created) and use those id's when inserting each new (corresponding row) in the 'map_locations' table. 现在,我想从“ data_items”(刚创建的自动增量)中获取每个ID,并在“ map_locations”表中插入每个新的(对应行)时使用这些ID。
So, 所以,
data_items
id | data_source_id
1 | 1
2 | 1
3 | 2
4 | 2
map_locations
id | content
1 | aaa
2 | bbb
....
3 | aaa
4 | bbb
I need to copy row (1, 2), use the id from data_items (3,4) and insert those rows. 我需要复制行(1、2),使用data_items(3,4)中的ID并插入这些行。
Hope this makes sense..it's rather confusing. 希望这是有道理的。 TIA.
TIA。
You need the newly created data_items
to reference their original counterparts; 您需要新创建的
data_items
引用其原始副本。 the easiest way to do this would be to modify the schema so that the table includes a self-referencing foreign key: 最简单的方法是修改架构,以便表包含自引用外键:
ALTER TABLE data_items ADD COLUMN underlying_id INT(11) NULL, ADD FOREIGN KEY (underlying_id) REFERENCES data_items (id)
The insertion into data_items
then becomes: 然后插入到
data_items
变为:
INSERT INTO data_items (underlying_id, data, created, updated, data_item_type, data_source_id) SELECT id, data, NOW(), NOW(), data_item_type, 2 FROM data_items WHERE data_source_id = 1
One can now perform a self-join to obtain the newly created id
s: 现在,可以执行自联接以获得新创建的
id
:
INSERT INTO map_locations (id, content) SELECT new.id, map_locations.content FROM map_locations JOIN data_items old USING (id) JOIN data_items new ON new.underlying_id = old.id WHERE old.data_source_id = 1
Ensure that you perform this query within the same transaction as the previous insertion, using the (default) REPEATABLE READ
transaction isolation level, to ensure that any concurrent changes to the data_items
table don't lead to inconsistent state. 确保使用(默认)
REPEATABLE READ
事务隔离级别在与上一次插入相同的事务中执行此查询,以确保对data_items
表的任何并发更改都不会导致不一致的状态。
Again, within the same transaction, clear the underlying_id
reference: 同样,在同一事务中,清除
underlying_id
参考:
UPDATE data_items SET underlying_id = NULL
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.