简体   繁体   中英

MySQL: Match same string in two tables and on match change 1 field based on the other using temporary table and csv

Currently i have a table filled with skus, products quantity and comments. I've added a new field called locations to it.

I have a csv that i exported from our inventory software which contains only the skus, names and locations.

Because the sku's are different, i'm looking to use the product names from the csv and match them against the current table. If it matches i want the location field to update.

I've tried several different versions of this, however each time it throws off my table. I'm not sure if it's the CSV or the mysql.

Here is my MySQL statement:

CREATE TEMPORARY TABLE products_clone_table LIKE product_list_clone;

LOAD DATA LOCAL INFILE '/home/account/product_locations.csv' INTO TABLE products_clone_table FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\n' (SKU, PRODUCT_NAME, LOCATION);

UPDATE product_list

JOIN products_clone_table ON products_clone_table.PRODUCT_NAME = product_list.PRODUCT_NAME

SET product_list.LOCATION = products_clone_table.LOCATION;

DROP TEMPORARY TABLE products_clone_table;"

For some reason when i use this im finding it's throwing my tables awry. The only way i can describe it is that terminals showing it as squiggly and data is being removed. Some of the SKU's are missing, as are the locations that are supposed to match up.

If anyone has any ideas please let me know. What am i doing wrong here?

The issue was that the CSV itself. Take note, to anyone struggling like i did, make sure you quote your fields on export to csv and set the end of line to be /r/n and not just /n .

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