我正在尝试在mysql中执行LOAD DATE INFILE,并且我尝试将数据从其中一个已加载的列转换为基于我已在数据库中设置的表的数字。以下是我目前的查询。
LOAD DATA LOCAL INFILE 'path\\Grand Export.csv'
INTO TABLE student
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS
(fameid, @ssn, @sname, @dummy, @dummy, @dummy, @dummy,
@dummy, @dummy, address, city, state, @dummy, zip, @dummy,
@dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy,
@dummy, @dummy, @dob, @gender, @ethnicity, @dummy, @dummy)
SET
ssn = concat(LEFT(@ssn,3),MID(@ssn,5,2),RIGHT(@ssn,4)),
fname = substring_index(trim(both """" FROM @sname),',',1),
lname = substring_index(TRIM(BOTH """" FROM @sname),',',-1),
gender = LEFT(@gender,1),
dob = date_format(str_to_date(@dob,'%m/%d/%Y'),'%Y-%m-%d'),
ethid = CALL getethid(@ethnicity);
问题是getethid功能不起作用。它的代码如下所示:
CREATE DEFINER=`root`@`localhost` FUNCTION `getethid`(eth VARCHAR(40))
RETURNS INT
BEGIN
DECLARE @valethid INT;
SELECT ethid FROM fameEth WHERE ethnicity = eth
INTO @valethid;
RETURN @valethid;
END
每次我尝试运行它时都会告诉我语法错误。我在这里做错了什么?
更新:我调整了现在遇到新问题的代码:
加载以下数据代码:
LOAD DATA LOCAL INFILE 'C:\\ProgramData\\MySQL\\MySQL Server 5.7\\Uploads\\Grand Export.csv'
INTO TABLE student
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS
(fameid, @ssn, @sname, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, address, city,
state, @dummy, zip, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy,
@dummy, @dummy, @dob, @gender, @ethnicity)
SET
ssn = concat(LEFT(@ssn,3),MID(@ssn,5,2),RIGHT(@ssn,4)),
fname = substring_index(trim(both """" FROM @sname),',',1),
lname = substring_index(TRIM(BOTH """" FROM @sname),',',-1),
gender = LEFT(@gender,1),
dob = date_format(str_to_date(@dob,'%m/%d/%Y'),'%Y-%m-%d'),
ethid = getethid(@ethnicity);
此处的功能代码:
CREATE DEFINER=`root`@`localhost` FUNCTION `getethid`(eth VARCHAR(40))
RETURNS int(11)
BEGIN
SELECT ethid FROM ethnicity WHERE ethnicity = eth
INTO @valueid;
RETURN @valueid;
END
现在执行LOAD DATA查询,但未正确填充ethid值。它不识别某些类型的数据,因此在后面的行中默认为先前的数据值。
答案 0 :(得分:0)
调用函数时是否应该使用“CALL”?我以为那只是存储过程。尝试删除单词CALL,留下“ethid = getethid(@ethnicity)”。
答案 1 :(得分:0)
看起来我让它运转了。这是最终的代码:
创建函数如下:
CREATE DEFINER=`root`@`localhost` PROCEDURE `getethid`(IN eth VARCHAR(40), OUT newid INT)
BEGIN
SELECT ethid FROM fameEth WHERE ethnicity = eth;
END
(这是使用MySQL工作台的想法)
上传代码如下:
LOAD DATA LOCAL INFILE 'C:\\ProgramData\\MySQL\\MySQL Server 5.7\\Uploads\\Grand Export.csv'
INTO TABLE student
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS
(fameid, @ssn, @sname, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, address, city,
state, @dummy, zip, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy,
@dummy, @dummy, @dob, @gender, @ethnicity, @dummy, @dummy, @dummy, @dummy)
SET ssn = concat(LEFT(@ssn,3),MID(@ssn,5,2),RIGHT(@ssn,4)),
fname = substring_index(trim(both """" FROM @sname),',',1),
lname = substring_index(TRIM(BOTH """" FROM @sname),',',-1),
gender = LEFT(@gender,1),
dob = date_format(str_to_date(@dob,'%m/%d/%Y'),'%Y-%m-%d'),
ethid = getethid(@ethnicity);
我遇到的最后一个问题是由于我的参考表中缺少值。一旦我更新了它,getethid()函数就能完美运行。
感谢所有人的帮助!