第一次插入和同一日期的最后一次插入的单日时间戳差异

时间:2012-10-05 11:54:14

标签: mysql sql timestamp

以下是查询

SELECT
cap_newspaper_page.capnewspaperID,
cap_newspaper_page.capnewspaperPageID,
cap_newspaper.newspaperStationID,
cap_newspaper_page.insertDate
FROM
cap_newspaper_page

我写了一个查询结果如下! 但问题是我想要每天的总时差。就像我希望6月27日的总持续时间以及其他日期一样。请帮我解决这个问题。

enter image description here

I want Time difference from -2012-06-27 18:54:27- To -2012-06-27 19:26:58-
and respectively for other dates! 

Click Here for LARGER IMAGE

3 个答案:

答案 0 :(得分:2)

你可以使用date_diff。

将日期保存到变量中。 那么,如果你要比较今天所有,

    $insert_date=$sql['insertDate'];
    $now = date_create("now");
$compDate = date_create(date($insert_date));
$interval = date_diff($compDate,$now);

编辑: 如果您正在运行while循环来显示数据 - 您可以简单地使用数组来保存每条记录的时间,然后您可以根据您希望使用的两者之间的差异显示时间。

如果我理解正确,那就与此类似;

while ($row=mysql_fetch_array($results)) {
$i=$i+1;
$insert_date[$i] = $row['insertDate'];
if ($i!=0){
$interval = date_diff($insert_date[$i],$insert_date[$i-1]);
}
echo "<tr><td>" . $row['insertDate'] . "</td><td>" . $interval . "</td></tr>";
}

答案 1 :(得分:0)

SELECT
cap_newspaper_page.capnewspaperID,
cap_newspaper_page.capnewspaperPageID,
cap_newspaper.newspaperStationID,
(select sum(TIME(cap_newspaper_page.insertDate)) from cap_newspaper_page a where DATE(a.insertDate) = DATE(b.insertDate)
group by DATE(a.insertDate))
FROM
cap_newspaper_page b

答案 2 :(得分:0)

让我知道这个是否有效:

SELECT
IF(@tmpDate != DATE(q.insertDate), @firstDate := DATE(q.insertDate), @firstDate := @firstDate) as seeIfDateChanged, @tmpDate := q.insertDate, q.*, TIMEDIFF(q.insertDate, @firstDate) AS timeDifference
FROM (
SELECT
*
FROM
yourTable yt
, (SELECT @firstDate:=NULL, @tmpDate:=NULL) r
ORDER BY insertDate
) q