日期:2014-05-16  浏览次数:20970 次

关于LOAD DATA LOCAL INFILE
有这样一个文件users.txt
HTML code

user001中文    e10adc3949ba59abbe56e057f20f883e    NULL
user002    e10adc3949ba59abbe56e057f20f883e    NULL
user003    e10adc3949ba59abbe56e057f20f883e    NULL
user004    e10adc3949ba59abbe56e057f20f883e    NULL
user005    e10adc3949ba59abbe56e057f20f883e    NULL
user006    e10adc3949ba59abbe56e057f20f883e    NULL
user007    e10adc3949ba59abbe56e057f20f883e    NULL
user008    e10adc3949ba59abbe56e057f20f883e    NULL
user009    e10adc3949ba59abbe56e057f20f883e    NULL
user010    e10adc3949ba59abbe56e057f20f883e    NULL
user008    e10adc3949ba59abbe56e057f20f883e    NULL
user009    e10adc3949ba59abbe56e057f20f883e    NULL
user010    e10adc3949ba59abbe56e057f20f883e    NULL


用下面的语句导入到users表中,
SQL code

LOAD DATA LOCAL INFILE "D:/users.txt"
                    INTO TABLE users 
                    FIELDS TERMINATED BY "\t" ENCLOSED BY ""                
                    LINES TERMINATED BY "\n" STARTING BY "" (username, password, avatar)



但是有个问题,被导入的数据,其中NULL值,被做为字符串"NULL"导了进去,
有没有办法把NULL这一列做为mysql的NULL,而不是字符串null导入?

------解决方案--------------------
SQL code
LOAD DATA LOCAL INFILE "D:/users.txt"
    INTO TABLE users 
    FIELDS TERMINATED BY "\t" ENCLOSED BY ""                
    LINES TERMINATED BY "\n" STARTING BY "" 
    (username, password, @x)
    set avatar=if(@x='NULL',null,@x);