获取开始和结束时间mysql之间的一分钟间隔

时间:2018-04-13 10:58:56

标签: mysql

Id    |    starttime        |    endtime          |
 1    |    1999-05-07 15:00 |    1999-05-07 16:45 |

如何从开始时间到结束时间间隔1分钟。

所以我想要这样的15:0115:02

请任何帮助将不胜感激sql并尝试尽我所能学习它。感谢

注意:

我不想要整整一分钟。我想从头到尾得到每一分钟我需要它来计算在游戏的每一分钟登录的用户如果有任何意义

2 个答案:

答案 0 :(得分:1)

SELECT *,TIMESTAMPDIFF(MINUTE,starttime,endtime) FROM Table1

创建此功能

CREATE function getTimeAll(id1 int)
RETURNS VARCHAR(65535) DETERMINISTIC
BEGIN
  DECLARE time1 varchar(65535);
  DECLARE intrvlMin int;
  DECLARE c int;
  SET intrvlMin=(SELECT TIMESTAMPDIFF(MINUTE,starttime,endtime) FROM Table1 WHERE id = id1);
  SET c=1;
  SET time1 = '';
  While c < intrvlMin
  do
    SET time1=CONCAT(time1,(SELECT date_format(date_add(starttime,interval c minute),'%H:%i') FROM Table1 WHERE id = id1),',');
    SET c = c + 1;
  end WHILE;
  SET time1=CONCAT(time1,(SELECT date_format(date_add(starttime,interval c minute),'%H:%i') FROM Table1 WHERE id = id1));
  RETURN time1;
END ;

然后运行

SELECT `Id`, `starttime`, `endtime`,getTimeAll(`Id`) AS Times FROM Table1;

<强>输出

Id  starttime           endtime             Times
1   1999-05-07 15:00:00 1999-05-07 16:45:00 15:01,15:02,15:03,15:04,15:05,15:06,15:07,15:08,15:09,15:10,15:11,15:12,15:13,15:14,15:15,15:16,15:17,15:18,15:19,15:20,15:21,15:22,15:23,15:24,15:25,15:26,15:27,15:28,15:29,15:30,15:31,15:32,15:33,15:34,15:35,15:36,15:37,15:38,15:39,15:40,15:41,15:42,15:43,15:44,15:45,15:46,15:47,15:48,15:49,15:50,15:51,15:52,15:53,15:54,15:55,15:56,15:57,15:58,15:59,16:00,16:01,16:02,16:03,16:04,16:05,16:06,16:07,16:08,16:09,16:10,16:11,16:12,16:13,16:14,16:15,16:16,16:17,16:18,16:19,16:20,16:21,16:22,16:23,16:24,16:25,16:26,16:27,16:28,16:29,16:30,16:31,16:32,16:33,16:34,16:35,16:36,16:37,16:38,16:39,16:40,16:41,16:42,16:43,16:44,16:45

<强>样本

  

https://www.db-fiddle.com/f/nge35TSTWyd3pLSw6X1TE4/2

答案 1 :(得分:0)

您可能需要使用循环

首先

select TIMESTAMPDIFF(minute,starttime,endtime)  from table1

在这里你将获得105

计算2次之间的分钟数

SELECT date_format(date_add(starttime,interval 1 minute),'%H:%i') from table1
SELECT date_format(date_add(starttime,interval 2 minute),'%H:%i') from table1
SELECT date_format(date_add(starttime,interval 3 minute),'%H:%i') from table1

以递增间隔时间将此循环至105次