简体   繁体   中英

How to import data.csv file to phpmyadmin(mysql) having columns with datetime format?

I need to import data from a.csv file to MySQL. The data is imported successfully if I use varchar instead of the timestamp as the datatype for date-time columns. I need it to be in the data-time format in the database too. I've various columns and here I have just shown a snippet of data.

So I have this data:

| region | start_year |          added         |       published        |
| ------ | ---------- | ---------------------- | ---------------------- |
|        |   2017     | June, 26 2018 07:28:39 | June, 26 2018 00:00:00 |
|        |   2010     | June, 26 2018 05:53:45 | June, 26 2018 00:00:00 |
| World  |            | June, 26 2018 03:53:23 | June, 26 2018 00:00:00 |
| World  |            | June, 26 2018 06:50:48 | June, 26 2018 00:00:00 |
|        |            | June, 26 2018 02:59:51 | June, 26 2018 00:00:00 |
|        |            | June, 26 2018 04:47:28 | June, 26 2018 00:00:00 |
| Europe |            | June, 26 2018 05:31:29 | June, 26 2018 00:00:00 |
|        |            | June, 26 2018 04:47:28 | June, 26 2018 00:00:00 |
|        |            | June, 26 2018 01:33:47 | June, 26 2018 00:00:00 |
|        |            | June, 26 2018 04:14:57 | June, 26 2018 00:00:00 |

A snippet of actual data is here: 数据片段

Here is a snippet of the schema: Mysql 中的模式片段

(Please be mindful that there are more columns than I've given a sample of) I need to import this type of data to my MySQL database having table_name = data. The datatype I'm using for both added and published is timestamp() . I tried this:

LOAD DATA INFILE 'C:/Users/rasha/Downloads/Data.csv'
INTO TABLE data
FIELDS TERMINATED BY ';'
LINES TERMINATED BY '\n'
(region, start_year, @date_time_variable, @date_time_variable2)
SET added = STR_TO_DATE(@date_time_variable, '%b-%d-%Y %H:%M:%S'), published = STR_TO_DATE(@date_time_variable2, '%b-%d-%Y %H:%M:%S');

Now the above query does run successfully but all other fields get empty or 0 as the value and the added field has current_timestamp() as a value which by the way is the default value and published gets a null value which is its default value.

I'm using this in my laravel project. Help me import this data in the correct datetime format because I need to compare dates and times in the future for chart preparation. Thank you!

Your format string for your dates is completely wrong.

Try this:

SELECT STR_TO_DATE('June, 26 2018 07:28:39', '%M, %e %Y %H:%i:%s') -- 2018-06-26 07:28:39

The complete list of format identifiers is available here . Be sure to use the correct identifier, and correct case, to match your source data. You also need to match separators and whitespace. Watch out for abbreviated forms of month and day names, leading zeroes on the numeric days, and AM/PM or 24-hour formats.

See https://www.db-fiddle.com/f/dyydumkv4Ztmp6TogKk4VM/0

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