Mysql查询到CSV和一个字段拆分

时间:2013-02-28 11:21:52

标签: mysql csv split

我有这个代码,但它有问题:

SELECT email, realname INTO OUTFILE "c:/mydata.csv"
SUBSTRING_INDEX(SUBSTRING_INDEX(realname, ' ', 1), ' ', -1) as firstname,
SUBSTRING_INDEX(SUBSTRING_INDEX(realname, ' ', 2), ' ', -1) as lastname,
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY "\n"
FROM mytable;

我只选择电子邮件和真实姓名字段,并在遇到空格时将真实姓名拆分为2个字段!

错误:

 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SUBSTRING_INDEX(SUBSTRING_INDEX(realname, ' ', 1), ' ', -1) as firstname, SUBST' at line 2

1 个答案:

答案 0 :(得分:0)

INTO OUTFILE必须在最后。

SELECT email, realname 
SUBSTRING_INDEX(SUBSTRING_INDEX(realname, ' ', 1), ' ', -1) as firstname,
SUBSTRING_INDEX(SUBSTRING_INDEX(realname, ' ', 2), ' ', -1) as lastname,
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY "\n"
FROM mytable
INTO OUTFILE "c:/mydata.csv";

See manual entry.