使用LOAD DATA INFILE为日期格式的多个SET字段

时间:2013-02-14 02:30:39

标签: php mysql

我可以使用LOAD DATA INFILE成功上传我的csv文件。我可以跳过我不需要的所有数据/列(Manual 13.2.6

我能够使用SET命令设置日期格式。我的问题是,如果我有多个日期字段,当我有超过1个SET时,SET命令的任何变化与上述相结合都不希望工作。

<?php 

if (isset($_POST['load']))
{
include '_inc/include.php'; 

$temp = $_FILES['myfile']['tmp_name'];
$sqlstatement="LOAD DATA LOCAL INFILE '$temp' INTO TABLE 000_1616 FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES (id, @somedate, name, anotherdate, color, 1moredate) SET sdate = IF(LENGTH(@somedate)=7,STR_TO_DATE(@somedate,'%m/%d/%Y'),STR_TO_DATE(@somedate,'%m/%d/%y'))";
mysql_query($sqlstatement) or die(mysql_error()); 

echo "It worked";
echo "<p><a href='upload-display.php'>go to page</a></p>";
} 

?>

<form method="post" action="upload.php" enctype="multipart/form-data">
<input name="myfile" type="file" />
<input name="load" type="submit" value="submit" /></form>

我的SET就像这样

SET sdate = IF(LENGTH(@somedate)=7,STR_TO_DATE(@somedate,'%m/%d/%Y'),STR_TO_DATE(@somedate,'%m/%d/%y'))";

$sqlstatement="LOAD DATA LOCAL INFILE '$temp' INTO TABLE 000_1616 FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES (id, @somedate, name, anotherdate, color, 1moredate) SET sdate = IF(LENGTH(@somedate)=7,STR_TO_DATE(@somedate,'%m/%d/%Y'),STR_TO_DATE(@somedate,'%m/%d/%y'))";

我使用@somedate作为第一个日期字段,这是真正的sdate。只要我不尝试使用SET添加4个日期字段,这样就可以正常工作。我想添加@anotherdate(anotherdate)和@ 1moredate(1moredate)我是否必须设置一个数组?我是否需要在phpMyADMIN端的表中执行某些操作?现在它们被格式化为Date,但除了提交的第一个日期之外,它们都会返回000-00-00。

这是SQL:

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `database_track`
--

-- --------------------------------------------------------

--
-- Table structure for table `000_datetest`
--

CREATE TABLE IF NOT EXISTS `000_datetest` (
  `id` int(11) NOT NULL auto_increment,
  `sdate` date default NULL,
  `name` varchar(100) collate utf8_unicode_ci default NULL,
  `anotherdate` date NOT NULL,
  `color` varchar(50) collate utf8_unicode_ci NOT NULL,
  `1moredate` date NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=6 ;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

csv示例:

+----+---------------+-------------------+---------------------+------------+-------------------+
| id | sdate          | name             | anotherdate     | color        | 1moredate     |
+----+---------------+------------------------+---------------+------------+-------------------+
|  1 | 2011-08-21 | Tom Thumb |    08/16/2010     |   Blue     | 1/5/08            |
+----+---------------+------------------------+---------------+------------+-------------------+
|  1 | 2009-05-12 | Don Duck      |    03/22/2012     |   Yellow   | 9/15/03       |
+----+---------------+------------------------+---------------+------------+-------------------+
1 row in set (0.00 sec)

我希望这有帮助,请参阅以下评论中的实体文件

1 个答案:

答案 0 :(得分:0)

要在LOAD DATA INFILE中使用多个SET命令,您只需指定SET一次并用逗号分隔每个变量定义,例如。

LOAD DATA INFILE 'my_file' INTO TABLE 'my_table' (column1, @date1, @date2)
SET column2 = STR_TO_DATE(@date1, '%d/%m/%Y %H:%i:%s'),
column3 = STR_TO_DATE(@date2, '%d/%m/%Y %H:%i:%s');