我的csv内容如下:
1,01.03.13,6310086579
2,01.03.13,6310086569
3,01.03.13,6310086573
4,04.03.13,6310086592
5,04.03.13,6310086592
6,04.03.13,6310086592
7,05.03.13,6310086633
8,05.03.13,6310086639
我必须将日期01.03.13
输入2013-03-01
到MySQL表中,并且它不起作用。类似于以下内容:
LOAD DATA INFILE 'E:\test.csv'
INTO TABLE test
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
(sno, sdate, orderno, @d1)
SET sdate = date_format(str_to_date(@d1, '%d.%m.%y'), '%Y-%m-%d')
答案 0 :(得分:0)
@ d1始终为null,因为您的csv文件中只有3列。我想您打算将csv文件中的第二列分配给@ d1。
DROP TABLE IF EXISTS T;
CREATE TABLE T (sno INT,sdate DATETIME,orderno varchar(20));
LOAD DATA INFILE 'C:\\Program Files\\MariaDB 10.1\\data\\sandbox\\data.txt'
INTO TABLE t
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
(sno, @d1, orderno)
SET sdate = date_format(str_to_date(@d1, '%d.%m.%y'), '%Y-%m-%d');
;
select * from t;
+------+---------------------+-------------+
| sno | sdate | orderno |
+------+---------------------+-------------+
| 1 | 2013-03-01 00:00:00 | 6310086579
| 2 | 2013-03-01 00:00:00 | 6310086569
| 3 | 2013-03-01 00:00:00 | 6310086573
| 4 | 2013-03-04 00:00:00 | 6310086592
| 5 | 2013-03-04 00:00:00 | 6310086592
| 6 | 2013-03-04 00:00:00 | 6310086592
| 7 | 2013-03-05 00:00:00 | 6310086633
| 8 | 2013-03-05 00:00:00 | 6310086639 |
+------+---------------------+-------------+
8 rows in set (0.00 sec)
但我认为您根本不需要转换第2列。而且,您应该查看https://dev.mysql.com/doc/refman/8.0/en/load-data.html,尤其是“ SET子句中的用户变量可以以多种方式使用”开头的部分