- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的商店中有 500 个信息亭,并且有一个表可以通过商店 ID 跟踪每个打印件的时间戳。我需要生成一个记录集,以秒为单位给出打印之间的时间。所有这些数据都存储在一个表中。每次打印都会插入一条记录,其中包含商店 ID 和时间戳。
表名=打印
id store_id 时间戳
1 1 2013-3-1 00:00:01
2 2 2013-3-1 00:00:01 *
3 3 2013-3-1 00:00:01
4 2 2013-3-1 00:00:12 *
5 3 2013-3-1 00:00:06
6 2 2013-3-1 00:00:15 *
我需要在所有商店 #2 打印之间拉出时间(以秒为单位)。 * 是为了让您可以轻松找到我需要比较的记录。
记录集结果如下:
id store_id myTimeDiffSeconds
2 2 0
4 2 11
6 2 3
这需要简单且快速。我可以在没有临时表的情况下执行此操作吗?
最佳答案
您可以通过两种方式编写查询
1.使用关联查询
2.使用 session 变量
第一个正是ethrbunny指出的
mysql> SELECT t1.id,
t1.store_id,
timestampdiff(second,IFNULL( (SELECT MAX(t2.timestamp)
FROM print t2
WHERE t2.store_id=2
AND t2.timestamp< t1.timestamp)
,t1.timestamp),t1.timestamp) myTimeDiffSeconds
FROM print t1
WHERE t1.store_id=2 ORDER BY t1.timestamp;
+------+----------+-------------------+
| id | store_id | myTimeDiffSeconds |
+------+----------+-------------------+
| 2 | 2 | 0 |
| 4 | 2 | 11 |
| 6 | 2 | 3 |
+------+----------+-------------------+
3 rows in set (0.00 sec)
另一种方法是使用 session 变量来保存前一次的时间,但在这种情况下我们需要获取第一次的最小时间戳
mysql> select min(p.timestamp) into @prev_time from print p where p.store_id=2;
Query OK, 1 row affected (0.00 sec)
mysql> select id,
store_id,
timestampdiff(second,@prev_time,timestamp) myTimeDiffSeconds,
@prev_time:=timestamp
from print
where store_id=2 order by timestamp;
+------+----------+-------------------+---------------------+
| id | store_id | myTimeDiffSeconds | @prev_time:=t |
+------+----------+-------------------+---------------------+
| 2 | 2 | 0 | 2013-03-01 00:00:01 |
| 4 | 2 | 11 | 2013-03-01 00:00:12 |
| 6 | 2 | 3 | 2013-03-01 00:00:15 |
+------+----------+-------------------+---------------------+
3 rows in set (0.00 sec)
(timestamp,store_id) 上的索引将使查询执行得更好。
关于单个表的行中时间戳之间的 mySql TimeDiff,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15620476/
给定一个带有多个 date_time 戳的字符串,我想 提取第一个戳及其前面的文本 候选字符串可以有一个或多个时间戳 后续的 date_time 戳记将被 sep="-" 隔开 后续date_time
是否可以合并从相机拍摄的文本和照片?我想在照片上标记日期和时间,但我在 Google 上找不到任何内容。 最佳答案 使用下面的代码来实现你所需要的。 Bitmap src = Bitm
有没有办法通过 Graph API 戳另一个用户?基于this post ,并使用 Graph Explorer ,我发布到“/USERID/pokes”,我已经授予它(Graph API 应用程序和
我有两个向左浮动的元素。一个是 body 的第一个 child ,另一个是容器的第一个 child ,容器是 body 的第二个 child 。 ...
我是一名优秀的程序员,十分优秀!