How do I find and replace in a CSV I'm importing using mysql

放肆的年华 提交于 2021-01-27 13:21:25

问题


I'm importing a CSV file into Heidi SQL. I've created the table using this code:

create table all_data
    (
        Keyword varchar(1000),
        Position int,
        Previous_Position int,
        Search_Volume int,
        KW_Difficulty float,
        URL varchar(1000),
        Post_title varchar(1000),
        Post_URL varchar(1000),
        Genre varchar(1000),
        Location varchar(1000),
        Avg_Daily_Visitors float,
        pageviews int
        )
;

but in the Avg_Daily_visitors column it has "\N" where there is no value. I've been importing the data with this code:

load data local infile 'C:/filepath.../All_Data.csv'
replace into table all_data
fields terminated by ','
    enclosed by '"'
    escaped by '"'
lines terminated by "\r\n"
ignore 1 rows
set
    Avg_Daily_Visitors = replace(Avg_Daily_Visitors,"\N",0),
    pageviews = replace(pageviews,"\N", 0)
;

but it's not replacing the values with 0, which is what I want to achieve. How do I make Heidi SQL replace "\N" with "0" on import?

Thanks.


回答1:


First assign the value you read to a variable, then work on that variable. For this you specify the columns of your destination table, but a variable instead of the column where you want to replace.

load data local infile 'C:/filepath.../All_Data.csv'
replace into table all_data
fields terminated by ','
    enclosed by '"'
    escaped by '"'
lines terminated by "\r\n"
ignore 1 rows
(column_1, column_2, @variable1, @variable2, column_5)
set
    Avg_Daily_Visitors = replace(@variable1,"\N",0),
    pageviews = replace(@variable2,"\N", 0)
;


来源:https://stackoverflow.com/questions/43890328/how-do-i-find-and-replace-in-a-csv-im-importing-using-mysql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!