douwei7471 2014-05-07 20:43
浏览 181
已采纳

使用LOAD DATA INFILE时,将CSV时间戳导入MySQL可接受的格式

I have a CSV file with the timestamp in the following format

 timestamp,             day_chan2, day_chan3
01/02/2014 00:00,             9,    2
01/02/2014 00:00,            16,    5

I am trying to import it into a MySQL database using LOAD DATA INFILE

$query_name = "LOAD DATA INFILE ' "
                                . $file_path . 
                                "' INTO TABLE '"
                                . $this->table_name . 
                                 " ' FIELDS TERMINATED BY '\,' 
                                 LINES TERMINATED BY '\
' 
                                 IGNORE 1 LINES 
                                 (`time_stamp`,`day_chan2`,`day_chan3`)";

My problem is the following: how do I convert the timestamp into a format acceptable to my MySQL while importing it into the database?

I am clueless now on how to change the timestamp into a proper timestamp which I can use to query later.

  • 写回答

2条回答 默认 最新

  • doudi8525 2014-05-07 20:53
    关注

    I've never tried this, but you might be able to do something like:

    $query_name = "LOAD DATA INFILE ' "
                                    . $file_path . 
                                    "' INTO TABLE '"
                                    . $this->table_name . 
                                     " ' FIELDS TERMINATED BY '\,' 
                                     LINES TERMINATED BY '\
    ' 
                                     IGNORE 1 LINES 
                                     (@mytimestamp,`day_chan2`,`day_chan3`)
                                     SET time_stamp=STR_TO_DATE(@mytimestamp, '%d/%m/%Y %h:%i');"
    

    There are examples of this in the MySQL documentation:

    http://dev.mysql.com/doc/refman/5.1/en/load-data.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教