简体   繁体   English

如何将时间存储为天跨度..as整数?

[英]How to store time as day span..as integer?

I had a table structure as follows.. column SrNo Arrival_Time and Day. 我的表结构如下:。列SrNo Arrival_Time和Day。 I want desired output as Day column in following pattern... How to store that? 我希望在以下模式中将所需的输出作为“ Day列...如何存储它? Using mysql query or php code snippet...? 使用mysql查询或php代码段...? Day value should be in integer format. 日期值应为整数格式。 Any help will be appreciated. 任何帮助将不胜感激。 Thanks in advance. 提前致谢。

CREATE TABLE `travel_time` (
  `id` int(11) NOT NULL auto_increment,
  `SrNo` int(3) default NULL,
  `Arrival_Time` time default NULL,
  `Day` int(1) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=61 ;

--
-- Dumping data for table `travel_time`
--

INSERT INTO `travel_time` (`id`, `SrNo`, `Arrival_Time`, `Day`) VALUES
(1, 1, '18:25:00', 1),
(2, 2, '19:50:00', 1),
(3, 3, '21:22:00', 1),
(4, 4, '22:23:00', 1),
(5, 5, '23:18:00', 1),
(6, 6, '00:30:00', 2),
(7, 7, '02:38:00', 2),
(8, 8, '03:20:00', 2),
(9, 9, '04:13:00', 2),
(10, 10, '04:53:00', 2),
(11, 11, '05:20:00', 2),
(12, 12, '06:55:00', 2),
(13, 13, '08:37:00', 2),
(14, 14, '10:10:00', 2),
(15, 15, '11:05:00', 2),
(16, 16, '11:22:00', 2),
(17, 17, '12:06:00', 2),
(18, 18, '13:22:00', 2),
(19, 19, '14:03:00', 2),
(20, 20, '14:40:00', 2),
(21, 21, '16:30:00', 2),
(22, 22, '17:28:00', 2),
(23, 23, '18:20:00', 2),
(24, 24, '19:15:00', 2),
(25, 25, '20:00:00', 2),
(26, 26, '21:18:00', 2),
(27, 27, '22:53:00', 2),
(28, 28, '23:51:00', 2),
(29, 29, '01:52:00', 3),
(30, 30, '04:10:00', 3),
(31, 1, '21:50:00', 1),
(32, 2, '23:30:00', 1),
(33, 3, '01:25:00', 2),
(34, 4, '02:27:00', 2),
(35, 5, '03:55:00', 2),
(36, 6, '05:45:00', 2),
(37, 7, '05:55:00', 2),
(38, 8, '06:43:00', 2),
(39, 9, '07:43:00', 2),
(40, 10, '09:00:00', 2),
(41, 11, '10:45:00', 2),
(42, 12, '11:40:00', 2),
(43, 13, '12:06:00', 2),
(44, 14, '13:37:00', 2),
(45, 15, '14:13:00', 2),
(46, 16, '14:29:00', 2),
(47, 17, '15:40:00', 2),
(48, 18, '16:43:00', 2),
(49, 19, '18:22:00', 2),
(50, 20, '19:22:00', 2),
(51, 21, '19:49:00', 2),
(52, 22, '20:24:00', 2),
(53, 23, '21:20:00', 2),
(54, 24, '21:53:00', 2),
(55, 25, '23:50:00', 2),
(56, 26, '01:13:00', 3),
(57, 27, '02:08:00', 3),
(58, 28, '03:27:00', 3),
(59, 29, '05:05:00', 3),
(60, 30, '06:50:00', 3);

I managed to get the query working, after about 45 minutes of work. 经过大约45分钟的工作,我设法使查询工作。 Here it is: 这里是:

SELECT id, SrNo, Arrival_Time,
    @day := if(m2 = 1, 1, if(m1 = 1, @day + 1, @day)) AS Day
FROM
(
    SELECT
        t1.id,
        t1.SrNo,
        t1.Arrival_Time,
        CASE WHEN t1.Arrival_time < t2.Arrival_time THEN 1 ELSE 0 END AS m1,
        CASE WHEN t1.SrNo < t2.SrNo THEN 1 ELSE 0 END AS m2
    FROM travel_time t1
    LEFT JOIN travel_time t2
        ON t1.id = t2.id + 1
) t1, (SELECT @day:=1) AS t
ORDER BY id;

Demo 演示版

The inner query, aliased as t1 , selects all data columns, along with two markers, m1 and m2 . 内部查询(别名为t1 )选择所有数据列以及两个标记m1m2 The m1 marker keeps track of whether we have rolled over to a new day, in which case the day needs to be incremented by one. m1标记会跟踪我们是否已滚动到新的一天,在这种情况下,需要将这一天增加一。 The m2 marker keeps track of whether we have reset the SrNo sequence, in which case the day also needs to be reset to 1. m2标记跟踪我们是否已重置SrNo序列,在这种情况下,还需要将日期重置为1。

Big lesson learned: We have to first materialize the self join before attempting to iterate with a user variable. 获得的重要教训: 尝试使用用户变量进行迭代之前,我们必须首先实现自我联接。 Without the t1 subquery trick, my solution kept failing. 没有t1子查询技巧,我的解决方案一直失败。 Check the demo below to see this in action. 查看下面的演示以查看实际操作。

Edit: 编辑:

If you need an actual table which contains this data, then I recommend using INSERT INTO ... SELECT with my query: 如果您需要一个包含此数据的实际表,那​​么我建议对查询使用INSERT INTO ... SELECT

INSERT INTO travel_time_new (id, SrNo, Arrival_Time, Day)
SELECT ...    -- my query from above

This requires creating a new table called travel_time_new which also contains a new column for the day. 这需要创建一个名为travel_time_new的新表,该表还包含当天的新列。 You may then delete the original travel_time table, and then rename the new table to the old one. 然后,您可以删除原始的travel_time表,然后将新表重命名为旧表。

I suggest this because updating your current table using my answer would require joining to my query, and that might have caveats as it is using user variables. 我建议这样做是因为使用我的答案更新当前表将需要加入我的查询,并且在使用用户变量时可能会有一些警告。

According to this question : how to calculate travel time using source and destination times? 根据这个问题: 如何使用来源时间和目的地时间计算旅行时间? I think you need a PHP script for this. 我认为您需要一个PHP脚本。

I wrote a code that you can set the time and run script.Then the Time you have set will be inserted to table. 我写了一个可以设置时间和运行脚本的代码,然后将您设置的时间插入到表中。 This code doesn't recognize if the hour is passed 23:59 because it will ruin the table since there is nothing more than 23:51 in your table and it's working! 这段代码无法识别时间是否已过23:59,因为它将破坏表格,因为表格中仅剩23:51,并且可以正常工作! This code just checks if the input time is after or before the last entered time in table then changes the day. 此代码仅检查输入时间是在表中最后输入的时间之后还是之前,然后更改日期。

I hope this is the code you are looking for: 我希望这是您要查找的代码:

<?php
$insertTime="07:10";
//Connect to Database
$conn=new mysqli($dbhost,$dbuser,$dbpass,$dbname);
if (!$conn) {
die("Error Connecting To Database: ".mysqli_connect_error()."<br/>");
}
$readlastdate="SELECT * FROM travel_time  ORDER BY `travel_time`.`id` DESC";
$readresult=mysqli_query($conn,$readlastdate);
while ($row=mysqli_fetch_assoc($readresult)) {
$time=date("H:i:s",strtotime($row['Arrival_Time']));
$readDay=$row['Day'];
$TimeToInsert=date("H:i:s",strtotime($insertTime));

if ($TimeToInsert < $time) {
    $insertday=$readDay+1;
        $sql="
        INSERT INTO travel_time (`SrNo`,`Arrival_Time`,`Day`)
        VALUES ('".($row['SrNo']+1)."','".$TimeToInsert."','".$insertday."' )
        ";
        if (mysqli_query($conn,$sql)) {
            break;
        }else{echo mysqli_error($conn)."<br/>";}
}else{
    $elsesql="INSERT INTO travel_time (`SrNo`,`Arrival_Time`,`Day`)
    VALUES ('".($row['SrNo']+1)."','".$TimeToInsert."','".$readDay."' )
    ";
}
}
?>

I hope this helps you 我希望这可以帮助你

PS:edit the database information PS:编辑数据库信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM