I know how to do the following in PHP, but I am wondering if it is possible to write a "pure" MySQL query to do the same:
Every night, I have a script which generates a CSV file of primary key IDs that I need to delete from a large database of records.
Is it possible to write a MySQL query that reads the CSV file, and deletes the record for each primary key value? In psuedocode, something along the lines of:
READ 'CSVFILE.csv' AS FILE;
FOR EACH( VALUE IN FILE )
DELETE FROM myTable WHERE id = VALUE;
Is something like this even possible just using a MySQL query?
You can do your job by using two queries and making a temporary table.
LOAD DATA INFILE
LOAD DATA INFILE 'CSVFILE.csv' INTO TABLE temp_tbl_name
FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n'
IGNORE 1 LINES;
DELETE FROM myTable WHERE id IN (SELECT toDeleteIdColumn FROM temp_tbl_name);
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.