mysqlimport工具CSV导入MySQL:列映射与特殊处理示例需求
mysqlimport (Mismatched Columns + Data Cleaning) Alright, let's work through this problem step by step. You're dealing with two key issues: mismatched column names between your CSV and MySQL table, and needing to strip the leading $ from the salary values. Since mysqlimport is just a command-line wrapper for MySQL's LOAD DATA INFILE statement, we can leverage that underlying functionality to handle both challenges.
First, Clarify the Column Mapping
Let's align your CSV columns to your TABLE_A columns clearly:
- CSV
id→TABLE_A.id(direct match) - CSV
first name→TABLE_A.name(name mismatch) - CSV
emp salary→TABLE_A.salary(name mismatch + needs$removed)
The mysqlimport Command to Handle Both Issues
We'll use user variables to temporarily store the raw CSV values, then transform the salary and map everything to the correct table columns. Here's the full command:
mysqlimport --local --user=your_db_user --password=your_db_password \ --columns='id,@temp_name,@temp_salary' \ --set='name=@temp_name,salary=SUBSTRING(@temp_salary, 2)' \ your_database_name /path/to/file.csv
Breakdown of Key Parameters:
--local: Tellsmysqlimportto read the CSV from your local machine (instead of looking for it on the MySQL server)--columns: Defines the order of columns in your CSV. We use@-prefixed variables for the columns that need mapping/cleaning (since we can't directly modify them here)--set: Maps the temporary variables to your actual table columns.SUBSTRING(@temp_salary, 2)removes the first character (the$) from the salary value before inserting it into thesalarycolumn- If your CSV uses quotes around values (e.g.,
"John","$500"), add--fields-enclosed-by='"'to handle that correctly
Equivalent LOAD DATA INFILE Statement (For Reference)
If you prefer running this directly in MySQL (instead of using the command-line tool), here's the equivalent SQL. It's often easier to tweak and test this way:
LOAD DATA LOCAL INFILE '/path/to/file.csv' INTO TABLE TABLE_A FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' (id, @temp_name, @temp_salary) SET name = @temp_name, salary = SUBSTRING(@temp_salary, 2);
Test with Your Sample Data
Let's verify with your example CSV rows:
1, John, $500 2, Mary Bell, $600
After running the command/query, TABLE_A will have these rows:
| id | name | salary |
|---|---|---|
| 1 | John | 500 |
| 2 | Mary Bell | 600 |
Critical Notes to Avoid Errors
- Ensure your MySQL user has the
FILEprivilege, and thatlocal_infileis enabled (runSET GLOBAL local_infile = 1;in MySQL if you get permission errors) - If your CSV has a header row (e.g.,
id,first name,emp salary), add--ignore-lines=1to themysqlimportcommand (orIGNORE 1 LINESin theLOAD DATAstatement) to skip it - Double-check the order of columns in
--columnsmatches exactly how they appear in your CSV—this is a common source of bugs
内容的提问来源于stack exchange,提问作者Anton Lieskovsky




