我的表格有一个“TIME”类型。它有2行,公司a具有以下值: 04:30:00 04:30:00 我希望使用代码
在csv文件中写入总共2行enter code here
$sql2 = "SELECT company, SUM(intervalTime)
INTO OUTFILE 'd:/myfile/xxx.csv'
FIELDS TERMINATED BY ','
FROM june2013 GROUP BY company";
if (mysqli_query($con,$sql2))
{
echo "Summary added to text file successfully";
}
else
{
echo "Error transferring summary " . mysqli_error($con);
}
将结果传输到具有以下值的csv文件: 公司一个86000 公司b 420000 公司c 292000
总计04:30:00 + 04:30:00应为9小时。我总共得到了4300 + 4300。 知道我是怎么解决这个问题的吗? 非常感谢!
答案 0 :(得分:1)
你不能像这样对时间列进行SUM,你需要先将它转换为数值(数学与数字一起使用)。
试试这个:
$sql2 = "SELECT company, SEC_TO_TIME( SUM( TIME_TO_SEC( intervalTime ) ) )
INTO OUTFILE 'd:/myfile/xxx.csv'
FIELDS TERMINATED BY ','
FROM june2013 GROUP BY company";
答案 1 :(得分:0)
我认为每家公司都有两排。然后,此查询可能会有所帮助:
select
t1.company AS com,
addtime(
(select intervalTime from june2013 AS t2 where t2.company = t1.company LIMIT 1),
(select intervalTime from june2013 AS t2 where t2.company = t1.company LIMIT 1, 1)
) AS dif
into outfile 'd:/myfile/xxx.csv'
FIELDS TERMINATED BY ','
from june2013 as t1
group by t1.company;
使用函数addtime(time1,time2)正确地对日期求和。请参阅mysql文档。