Reputation: 157
the table named: products
. it have five hundred rows record. the table have field :products_model
products_price
. the products_model
is unique。
the original value that stored in products
table are all 0.0000
. now i want to update it with batch in phpmyadmin according to the 'products_model`.
i prepared the data in excel
with two columns, 'products_modelthe new value
products_price`(according to some reason, some may be empty). how do i do? thank you.
the data form a csv file :
products_model products_price
LB2100
LB2117 12.49
LB2381 35.99
LB2307 12.99
LB2380 35.99
LB2468 10.99
LB2139
LB2223
LB2027 15.99
LB2126 12.99
LB2308 9.99
LB2124 13.99
LB2077
LB2361
LB2021 14.99
LB2129
LB2010 11.21
LB2186
LB2039 15.66
LB2153
LB2564 30.99
LB2104
LB1182 15.99
LB2034 15.9
LB2455 15.55
LB2189
LB2559 22.9
LB2382 35.99
LB2168 10.99
Upvotes: 0
Views: 1947
Reputation: 121922
Two steps:
Edit:
About the REPLACE keyword - From the reference - If you specify REPLACE, input rows replace existing rows. In other words, rows that have the same value for a primary key or unique index as an existing row.
The usage is the same -
LOAD DATA INFILE 'file_name' REPLACE INTO TABLE tbl_name FIELDS TERMINATED BY...
Code
Create and populate the table:
CREATE TABLE products(
products_model VARCHAR(6) NOT NULL,
products_price DECIMAL(5, 2) DEFAULT NULL,
PRIMARY KEY (products_model)
);
INSERT INTO products VALUES
('LB2307', 100.00),
('LB2380', 200.00),
('LB2468', 300.00);
Let's import data from CSV: Specify FIELDS TERMINATED, LINES TERMINATED and other paramaters for your task
LOAD DATA INFILE 'file_name.csv' REPLACE INTO TABLE products
FIELDS TERMINATED BY ' '
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES
(products_model, @var1)
SET products_price = IF(@var1 = '', 0, @var1); -- Replace empty string with 0
Check results:
SELECT * FROM products;
+----------------+----------------+
| products_model | products_price |
+----------------+----------------+
| LB1182 | 15.99 |
| LB2010 | 11.21 |
| LB2021 | 14.99 |
| LB2027 | 15.99 |
| LB2034 | 15.90 |
| LB2039 | 15.66 |
| LB2077 | 0.00 |
| LB2100 | 0.00 |
| LB2104 | 0.00 |
| LB2117 | 12.49 |
| LB2124 | 13.99 |
| LB2126 | 12.99 |
| LB2129 | 0.00 |
| LB2139 | 0.00 |
| LB2153 | 0.00 |
| LB2168 | 10.99 |
| LB2186 | 0.00 |
| LB2189 | 0.00 |
| LB2223 | 0.00 |
| LB2307 | 12.99 |
| LB2308 | 9.99 |
| LB2361 | 0.00 |
| LB2380 | 35.99 |
| LB2381 | 35.99 |
| LB2382 | 35.99 |
| LB2455 | 15.55 |
| LB2468 | 10.99 |
| LB2559 | 22.90 |
| LB2564 | 30.99 |
+----------------+----------------+
Good luck!
Upvotes: 1