简体   繁体   English

从两个表插入到一个表的SQL语句(使用来自另一个无连接条件的表的ID)

[英]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。

  1. 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) 
  2. 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 
  3. 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表的任何并发更改都不会导致不一致的状态。

  4. 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.

相关问题 使用select语句将记录从一个表插入到另一个表 - Insert record from one table to another table by using select statement SQL从一个表插入ID到另一个表中的条目 - Sql insert id from one table to entry in another 从一个表联接两个选择语句 - Join two select statement from one table MySQL:将数据从一个文件插入到两个表中,一个具有auto_increment id,另一个具有连接表 - MySQL: Insert data from one file into two tables, one with auto_increment id, and one join table PHP连接两个表,其中一个表中的两列包含from_ID和to_ID引用另一表中的id - PHP join two table where two column in one table contains from_ID and to_ID refer to id in another table 将一个表的最后一个ID插入另一个表 - Insert last id from one table into another SQL连接-一列用作另一表中两列的ID - SQL join - one column serving as ID for two columns in another table SQL从一个表列中选择一个最小值,然后将结果插入一个SQL语句的另一个表列中 - Sql Select a minimum value from a table column and insert the results in another table column in one SQL statement 如何使用 SQL 中的事务将数据从一个表插入到另一个表? - How to insert data from one table to another using transactions in SQL? 使用SQL查询将值从一个表插入另一个表 - INSERT values from one table to another using SQL Query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM