LOAD DATA INFILE将数据加载到不同的表中

时间:2016-02-23 05:17:18

标签: mysql sql

我有两张桌子:

Company(id int, varchar(name), primary key(id));
Product (id int, c_id int, varchar(name), foreign key(c_id), references Company(id));

表'公司'存储公司名称列表和'产品'存储产品名称列表,一个公司可以有多个产品。

如果我有这样的数据文件,则以制表符分隔:

1  Apple  iPhone
2  Apple  iPad
3  Apple  iMac
4  Google Gmail
5  Google Google Search
6  Amazon Kindle

是否可以使用"加载数据INFILE"将此文件加载到两个表中,第一列转到表公司,第二列转到Product?问题是如何将选定的字段加载到特定的表,而不是将完整的记录加载到一个表中。

2 个答案:

答案 0 :(得分:1)

我不相信LOAD DATA足够灵活,允许您有选择地将某些列加载到两个不同的已存在的表中。它被设计成一个快速工作的马,但设计不是特别灵活。另一种方法是将数据加载到临时表中,然后将INSERT INTO...SELECT数据加载到两个表中。

CREATE TABLE temp(id int, company varchar(55), product varchar(55));

LOAD DATA LOCAL INFILE 'file.csv' INTO TABLE temp
FIELDS TERMINATED BY ',' 
ENCLOSED BY '"' 
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES
(id, company, product);

然后只需使用INSERT INTO...SELECT将数据导入已存在的两个表中:

INSERT INTO Company (name)
SELECT company
FROM temp

INSERT INTO Product (name)
SELECT product
FROM temp

答案 1 :(得分:0)

您可以尝试这样的方法,前提是使用临时表。我希望你觉得它很有用。

/path/to/file/file.csv

1,Apple,iPhone
2,Apple,iPad
3,Apple,iMac
4,Google,Gmail
5,Google,Google Search
6,Amazon,Kindle
mysql> DELIMITER //

mysql> DROP TRIGGER IF EXISTS `from_load_data`//
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> DROP TABLE IF EXISTS `temp_company_product`//
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> DROP TABLE IF EXISTS `product`//
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> DROP TABLE IF EXISTS `company`//
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> CREATE TABLE `company` (
    ->     `id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    ->     `name` VARCHAR(25),
    ->     UNIQUE KEY `unique_name` (`name`)
    -> )//
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE `product` (
    ->     `id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    ->     `c_id` INT UNSIGNED NOT NULL,
    ->     `name` VARCHAR(25),
    ->     FOREIGN KEY (`c_id`) REFERENCES `company`(`id`)
    ->         ON UPDATE CASCADE ON DELETE CASCADE
    -> )//
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE `temp_company_product` (
    ->     `id` INT UNSIGNED PRIMARY KEY,
    ->     `company_name` VARCHAR(25),
    ->     `product_name` VARCHAR(25)
    -> )//
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TRIGGER `from_load_data` AFTER INSERT ON `temp_company_product`
    -> FOR EACH ROW
    -> BEGIN
    ->     INSERT INTO `company` (`name`) VALUES (NEW.`company_name`)
    ->         ON DUPLICATE KEY UPDATE `name` = VALUES(`name`);
    ->     INSERT INTO `product` (`c_id`, `name`)
    ->     SELECT `id`, NEW.`product_name`
    ->     FROM `company`
    ->     WHERE `name` = NEW.`company_name`;
    -> END//
Query OK, 0 rows affected (0.00 sec)

mysql> LOAD DATA INFILE '/path/to/file/file.csv'
    ->     INTO TABLE `temp_company_product`
    -> FIELDS TERMINATED BY ',' ENCLOSED BY '"'
    -> LINES TERMINATED BY '\r\n'
    -> (`id`, `company_name`, `product_name`)//
Query OK, 6 rows affected (0.00 sec)
Records: 6  Deleted: 0  Skipped: 0  Warnings: 0

mysql> SELECT
    ->     `id`,
    ->     `company_name`,
    ->     `product_name`
    -> FROM
    ->     `temp_company_product`//
+----+--------------+---------------+
| id | company_name | product_name  |
+----+--------------+---------------+
|  1 | Apple        | iPhone        |
|  2 | Apple        | iPad          |
|  3 | Apple        | iMac          |
|  4 | Google       | Gmail         |
|  5 | Google       | Google Search |
|  6 | Amazon       | Kindle        |
+----+--------------+---------------+
6 rows in set (0.00 sec)

mysql> SELECT
    ->     `id`,
    ->     `name`
    -> FROM
    ->     `company`//
+----+--------+
| id | name   |
+----+--------+
|  6 | Amazon |
|  1 | Apple  |
|  4 | Google |
+----+--------+
3 rows in set (0.00 sec)

mysql> SELECT
    ->     `id`,
    ->     `c_id`,
    ->     `name`
    -> FROM
    ->     `product`//
+----+------+---------------+
| id | c_id | name          |
+----+------+---------------+
|  1 |    1 | iPhone        |
|  2 |    1 | iPad          |
|  3 |    1 | iMac          |
|  4 |    4 | Gmail         |
|  5 |    4 | Google Search |
|  6 |    6 | Kindle        |
+----+------+---------------+
6 rows in set (0.00 sec)

mysql> DROP TRIGGER IF EXISTS `from_load_data`//
Query OK, 0 rows affected (0.00 sec)

mysql> DROP TABLE IF EXISTS `temp_company_product`//
Query OK, 0 rows affected (0.00 sec)

mysql> DELIMITER ;