简体   繁体   中英

Best way to load this data into MySQL

I have a table that has a primary key and four foreign keys. All the data I am using is in an Excel spreadsheet. I am not sure the best way to go about loading this data into my database.

For some of the other tables, I exported the Excel columns I needed as .csv files, then used Regular Expressions to format the data into INSERT statements.

The main table I am using looks like this:

Table 1
----------------------------------------------------
| ID    | Text    | category_ID    | f2     |f3    |
----------------------------------------------------
| 1     | Foo     | NULL           | NULL   |NULL  |
| 2     | Bar     | NULL           | NULL   |NULL  |
----------------------------------------------------

The next table is like this:

Table 2
-----------------------
| ID    | Category    |
-----------------------
| 1     | CatFoo      |
| 2     | CatBar      |
-----------------------

And my Excel is like this:

Excel
---------------------------------
| ID    | Text    | category    |
---------------------------------
| 1     | Foo     | CatFoo      |
| 2     | Bar     | CatFoo      |
| 3     | What    | CatBar      |
---------------------------------

My goal is that the field Category_ID in Table 1 get the ID number of the given category from table 2. To do this I need to read what the category is from the Excel and put the proper Category_ID . I'm confused how I can do this when I only have the text data from the Excel sheet.

Is there a way to check the text against the data in Table 2 find the ID that matches the text, and update the row in Table 1 accordingly?

I have been searching online and haven't found anything I can use, however I am very much a beginner to databases so any help is appreciated.

First thing you should do is to load all you data in the same database.

I assume your CSV format is properly formed, you can load the csv file into your SQL database with the LOAD DATA INFILE function of mySQL if you dont use mysql you can probably find a similar function for your database system.

you need first create the table structure who constrain the data after that you should be load you data with the query (no worry if you don't know the default path to mysql engine, mySQL will give it to you in the error message )

LOAD DATA INFILE 'path/YourCSVFILE.csv' INTO TABLE your table FIELDS TERMINATED BY '' LINES TERMINATED BY '\\n';

see below the full documentation (adjust it for you own csv format) http://dev.mysql.com/doc/refman/5.1/en/load-data.html

So after a lot of try with complex UPDATE JOIN query without success, I found a solution to by pass your issue.

Create a new table with all the data without your empty id category and replace it with the new value should be works if you dont have strong link between your tables

CREATE TABLE result SELECT table1.id, table1.text, table2.id as id_category, f1, f2FROM table1INNER JOIN excel ON excel.text = table1.textINNER JOIN table2 ON table2.category = excel.category

I put in lower case all the table fields cause is easier for me.

Hope it's Help

Regards

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