I would load each new file into a temporary table first. This give more options when inserting into the real table. Something along the lines of:
Update a specific field on key duplicate IF a certain condition is satisfied
-
01-04-2022 - |
Вопрос
I have a table to which I upload data from CSV files.
The fields are like the following:
StudentName | SubjectName| Result
---------------------------------
Adam | Math | Fail
Bob | History | Pass
Catherine | Math | Pass
Dave | Science | Fail
Evan | History | Pass
Primary key here is (StudentName,SubjectName)
I use the following code to get the data into the table:
Load data local infile 'C:\\Test.csv' INTO TABLE test_table
Fields terminated by ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '"'
Lines terminated by '\n'
ignore 1 lines;
There are more than one csv files that I need to import. There could be possible key duplicates. I am trying to update a certain field when a duplicate is encountered.
i.e.: If there is a duplicate, UPDATE field RESULT if : value for result in the duplicate entry is "Pass".
Each csv file would be for each day the tests are conducted.
If the student gets a pass at least once, the table should reflect that.
How should I change my code to make this happen?
Решение
Другие советы
The load script itself can not help you do the table insert and update together. You can first load your CSV file into temp table, then use script to do "Upsert" to your final table, after that truncate your temp table, or each time create and drop temp table. Add a link for your reference