【问题标题】:Timestamp difference of single day from Fisrt insert and last insert of same date同一日期的第一个插入和最后一个插入的单日时间戳差异
【发布时间】:2012-10-05 11:54:13
【问题描述】:

下面是查询

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

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

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

【问题讨论】:

    标签: mysql sql timestamp


    【解决方案1】:

    你可以使用 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>";
    }
    

    【讨论】:

    • 我仍然没有找到我的答案。我认为将编写一个月份的循环以在同一日期从第一个inserDate 和最后一个inserDate 获取时间。比如 2012-06-27 18:54:27-To-2012-06-27 19:26:58 (19:26:58-18:54:27=01:17:31)
    • 我编辑了我的回复,以便更好地描述答案。我希望它有所帮助。
    【解决方案2】:
    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
    

    【讨论】:

      【解决方案3】:

      让我知道这个是否有效:

      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-25
        • 2017-08-13
        • 2013-02-09
        • 2015-01-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多