load into mysql_Load file into mysql

问题

I need to load a tab delimited file into mysql database. My database is set up with columns: ID;A;B;C;D;E and my file is a dump of columns ID and D. How can I load this file into my db and replace just columns ID and D, which out changing the values of columns C and E. When I load it in now, columns C and E are changed to DEFAULT:NULL.

回答1:

I already answered a similar question like this here, but in your case, you'd want to load the csv into a temporary table, then use a simple update SQL statement to update the specific columbs from your temporary table to your production table.

回答2:

You can update specific column using this command:

UPDATE the_table

SET D=

WHERE ID=

Then run this command for each row in the tab-delimited file, replacing the D and ID values for each.

回答3:

You can use a stored procedure or a PHP program to do the needful.

For MySQL, the SP would need to open the file using load_file() and storing the value in a variable. Then the program needs to loop through by finding "\n" which stands for new line to get a whole line in a string.

Then the program needs to be find the first tab position using locate() and by using substring() get the first ID col. Then the program needs to find the 4th tab i.e. 3 more tabs by using locate() and its 3rd parameter. This will be the starting position of your D column. Then find the next tab character again using locate() and its 3rd parameter which will give you the end character of the D column. Using substring() get the content of the D column. Use a update command to update the row's D column using ID as the search key in the where clause.

Since the above will loop through all lines you will update all rows of data.

来源:https://stackoverflow.com/questions/10606343/load-file-into-mysql

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
load into 和 insert into 都是 MySQL 数据库中用于将数据插入表中的语句,但它们在使用方法和适用场景上有一些区别。 load into 语句用于将数据从一个文件中加载到表中,通常适用于大批量导入数据。其语法如下: ``` LOAD DATA [LOCAL] INFILE 'file_name' [REPLACE | IGNORE] INTO TABLE table_name [CHARACTER SET charset_name] [FIELDS [TERMINATED BY 'field_separator'] [[OPTIONALLY] ENCLOSED BY 'enclosure_character'] [ESCAPED BY 'escape_character'] ] [LINES [STARTING BY 'line_prefix'] [TERMINATED BY 'line_separator'] ] [IGNORE number LINES] [(column1, column2, ..., column_n)]; ``` 其中,file_name 是数据文件的路径,table_name 是要插入数据的表名,charset_name 是字符集,fields 用于指定字段的分隔符和边界符,lines 用于指定行分隔符和前缀符,ignore 可以用于忽略文件中的前几行,column1 到 column_n 则是要插入数据的列名列表。 而 insert into 语句则是将数据直接插入到表中,通常适用于少量数据的插入。其语法如下: ``` INSERT INTO table_name [(column1, column2, ..., column_n)] VALUES (value1, value2, ..., value_n); ``` 其中,table_name 是要插入数据的表名,column1 到 column_n 是要插入数据的列名列表,value1 到 value_n 则是要插入的数据值。 因此,load into 和 insert into 的主要区别在于使用场景和数据量大小。如果需要导入大量数据,通常使用 load into;如果数据量较小,则使用 insert into。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值