简体   繁体   中英

How to LOAD DATA INLINE from ID 1 into MYSQL Table Column

I have a table topic which is having id and name id is primary. It has already data from ID 1 to 6.

I added one new column other . I want to load data from list.txt file into it and that should be from ID 1 to 6.

My list.txt is having:

apple
banana
orange
kiwi
My table:

CREATE TABLE `topics` (
  `id` int(11) UNSIGNED NOT NULL,
  `name` varchar(255) DEFAULT NULL,
  `other` varchar(500) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

INSERT INTO `topics` (`id`, `name`, `other`) VALUES
(1, 'Accessories', ''),
(2, 'Cover', ''),
(3, 'Logos', ''),
(4, 'Story', ''),
(5, 'Editing', ''),
(6, 'Gaming', '');

ALTER TABLE `topics`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `id` (`id`),
  ADD KEY `name` (`name`);

ALTER TABLE `topics`
  MODIFY `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=7;

I tried but it adds after 6.

LOAD DATA INFILE 'list.txt' INTO TABLE topics
  FIELDS TERMINATED BY ''
  LINES TERMINATED BY '\r\n' (other);

在此处输入图像描述

it added from 7, instead I wanted it to be from 1

Load data infile is the equivalent of an INSERT and is behaving as expected. There is no update option. I suggest you load to a staging table then UPDATE to target. I do foresee a problem though since there is no way of knowing which row to update in target.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM