mysql--SQL编程(关于mysql中的日期,关于重叠) 学习笔记2.2
1.日期中的重叠问题
建表sessions:
CREATE TABLE `sessions` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`app` varchar(10) NOT NULL,
`usr` varchar(10) NOT NULL,
`starttime` time NOT NULL,
`endtime` time NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB
插入记录:
insert into sessions(app,usr,starttime,endtime) values('app1','user1','08:30','08:45');
insert into sessions(app,usr,starttime,endtime) values('app1','user2','09:00','09:30');
insert into sessions(app,usr,starttime,endtime) values('app1','user1','09:15','10:30');
insert into sessions(app,usr,starttime,endtime) values('app1','user2','09:15','09:30');
insert into sessions(app,usr,starttime,endtime) values('app1','user1','10:30','14:30');
insert into sessions(app,usr,starttime,endtime) values('app1','user2','10:45','11:30');
insert into sessions(app,usr,starttime,endtime) values('app1','user1','11:00','12:30');
insert into sessions(app,usr,starttime,endtime) values('app2','user1','08:30','08:45');
insert into sessions(app,usr,starttime,endtime) values('app2','user1','08:30','08:45');
insert into sessions(app,usr,starttime,endtime) values('app2','user2','09:00','09:30');
insert into sessions(app,usr,starttime,endtime) values('app2','user1','11:45','12:00');
insert into sessions(app,usr,starttime,endtime) values('app2','user2','12:30','14:00');
insert into sessions(app,usr,starttime,endtime) values('app2','user1','12:45','13:30');
insert into sessions(app,usr,starttime,endtime) values('app2','user2','13:00','14:00');
insert into sessions(app,usr,starttime,endtime) values('app2','user1','14:00','16:30');
insert into sessions(app,usr,starttime,endtime) values('app2','user2','15:30','17:00');
创建索引,加快查询速度:
mysql> create unique index idx_app_usr_s_e_key on sessions(app,usr,starttime,endtime,id);
Query OK, 0 rows affected (0.23 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> create index idx_app_s_e on sessions(app,starttime,endtime);
Query OK, 0 rows affected (0.22 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show index in sessions;
+----------+------------+---------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+----------+------------+---------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| sessions | 0 | PRIMARY | 1 | id | A | 2 | NULL | NULL | | BTREE | | |
| sessions | 0 | idx_app_usr_s_e_key | 1 | app | A | 16 | NULL | NULL | | BTREE | | |
| sessions | 0 | idx_app_usr_s_e_key | 2 | usr | A | 16 | NULL | NULL | | BTREE | | |
| sessions | 0 | idx_app_usr_s_e_key | 3 | starttime | A | 16 | NULL | NULL | | BTREE | | |
| sessions | 0 | idx_app_usr_s_e_key | 4 | endtime | A | 16 | NULL | NULL | | BTREE | | |
| sessions | 0 | idx_app_usr_s_e_key | 5 | id | A | 16 | NULL | NULL | | BTREE | | |
| sessions | 1 | idx_app_s_e | 1 | app | A | 16 | NULL | NULL | | BTREE | | |
| sessions | 1 | idx_app_s_e | 2 | starttime | A | 16 | NULL | NULL | | BTREE | | |
| sessions | 1 | idx_app_s_e | 3 | endtime | A | 16 | NULL | NULL | | BTREE | | |
+----------+------------+---------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
9 rows in set (0.00 sec)
重叠的分类:标示重叠,分组重叠,最大重叠
标示重叠:为每个会话标示出相同应用程序用户重叠及最大重叠会话数
mysql> select a.app,a.usr,a.starttime,a.endtime,b.starttime,b.endtime from sessions a,sessions b where a.app=b.app and a.usr=b.usr and (b.endtime>=a.starttime and b.starttime<=a.endtime);
+------+-------+-----------+----------+-----------+----------+
| app | usr | starttime | endtime | starttime | endtime |
+------+-------+-----------+----------+-----------+----------+
| app1 | user1 | 08:30:00 | 08:45:00 | 08:30:00 | 08:45:00 |
| app1 | user1 | 09:15:00 | 10:30:00 | 09:15:00 | 10:30:00 |
| app1 | user1 | 09:15:00 | 10:30:00 | 10:30:00 | 14:30:00 |
| app1 | user1 | 10:30:00 | 14:30:00 | 09:15:00 | 10:30:00 |
| app1 | user1 | 10:30:00 | 14:30:00 | 10:30:00 | 14:30:00 |
| app1 | user1 | 10:30:00 | 14:30:00 | 11:00:00 | 12:30:00 |
| app1 | user1 | 11:00:00 | 12:30:00 | 10:30:00 | 14:30:00 |
| app1 | user1 | 11:00:00 | 12:30:00 | 11:00:00 | 12:30:00 |
| app1 | user2 | 09:00:00 | 09:30:00 | 09:00:00 | 09:30:00 |
| app1 | user2 | 09:00:00 | 09:30:00 | 09:15:00 | 09:30:00 |
| app1 | user2 | 09:15:00 | 09:30:00 | 09:00:00 | 09:30:00 |
| app1 | user2 | 09:15:00 | 09:30:00 | 09:15:00 | 09:30:00 |
| app1 | user2 | 10:45:00 | 11:30:00 | 10:45:00 | 11:30:00 |
| app2 | user1 | 08:30:00 | 08:45:00 | 08:30:00 | 08:45:00 |
| app2 | user1 | 08:30:00 | 08:45:00 | 08:30:00 | 08:45:00 |
| app2 | user1 | 08:30:00 | 08:45:00 | 08:30:00 | 08:45:00 |
| app2 | user1 | 08:30:00 | 08:45:00 | 08:30:00 | 08:45:00 |
| app2 | user1 | 11:45:00 | 12:00:00 | 11:45:00 | 12:00:00 |
| app2 | user1 | 12:45:00 | 13:30:00 | 12:45:00 | 13:30:00 |
| app2 | user1 | 14:00:00 | 16:30:00 | 14:00:00 | 16:30:00 |
| app2 | user2 | 09:00:00 | 09:30:00 | 09:00:00 | 09:30:00 |
| app2 | user2 | 12:30:00 | 14:00:00 | 12:30:00 | 14:00:00 |
| app2 | user2 | 12:30:00 | 14:00:00 | 13:00:00 | 14:00:00 |
| app2 | user2 | 13:00:00 | 14:00:00 | 12:30:00 | 14:00:00 |
| app2 | user2 | 13:00:00 | 14:00:00 | 13:00:00 | 14:00:00 |
| app2 | user2 | 15:30:00 | 17:00:00 | 15:30:00 | 17:00:00 |
+------+-------+-----------+----------+-----------+----------+
26 rows in set (0.00 sec)
分组重叠:服务商可能允许多个session的连接,并把其计费统计为1次,这就是所谓的分组重叠,对于例子中应该把app1,user1在08:30--10:30合并算为一次会话.
如下:
mysql> select distinct app,usr,starttime as s from sessions as a where not exists(select * from sessions as b where a.app=b.app and a.usr=b.usr and a.starttime>b.starttime and a.starttime<=b.endtime);
+------+-------+----------+
| app | usr | s |
+------+-------+----------+
| app1 | user1 | 08:30:00 |
| app1 | user1 | 09:15:00 |
| app1 | user2 | 09:00:00 |
| app1 | user2 | 10:45:00 |
| app2 | user1 | 08:30:00 |
| app2 | user1 | 11:45:00 |
| app2 | user1 | 12:45:00 |
| app2 | user1 | 14:00:00 |
| app2 | user2 | 09:00:00 |
| app2 | user2 | 12:30:00 |
| app2 | user2 | 15:30:00 |
+------+-------+----------+
11 rows in set (0.01 sec)
mysql> select distinct app,usr,starttime as e from sessions as a where not exists(select * from sessions as b where a.app=b.app and a.usr=b.usr and a.endtime>=b.starttime and a.endtime<b.endtime);
+------+-------+----------+
| app | usr | e |
+------+-------+----------+
| app1 | user1 | 08:30:00 |
| app1 | user1 | 10:30:00 |
| app1 | user2 | 09:00:00 |
| app1 | user2 | 09:15:00 |
| app1 | user2 | 10:45:00 |
| app2 | user1 | 08:30:00 |
| app2 | user1 | 11:45:00 |
| app2 | user1 | 12:45:00 |
| app2 | user1 | 14:00:00 |
| app2 | user2 | 09:00:00 |
| app2 | user2 | 12:30:00 |
| app2 | user2 | 13:00:00 |
| app2 | user2 | 15:30:00 |
+------+-------+----------+
13 rows in set (0.00 sec)
创建视图:v_s和v_e
mysql> create view v_s as select distinct app,usr,starttime as s from sessions as a where not exists(select * from sessions as b where a.app=b.app and a.usr=b.usr and a.starttime>b.starttime and a.starttime<=b.endtime);
mysql> create view v_e as select distinct app,usr,starttime as e from sessions as a where not exists(select * from sessions as b where a.app=b.app and a.usr=b.usr and a.endtime>=b.starttime and a.endtime<b.endtime);
未完待续......
mysql--SQL编程(关于mysql中的日期,关于重叠) 学习笔记2.2的更多相关文章
- C#同步,异步的理解,包括5.0中await和async(学习笔记)
之前在工作中一直用的是同步线程,就是先进入画面的load事件,然后在里面进行数据库调用的处理.后面又遇到了公司软件中一些比较古老的代码,一开始在那块古老代码中增加机能的时候,我想用到数据库的数据给画面 ...
- Oracle使用触发器和mysql中使用触发器的比较——学习笔记
一.触发器 1.触发器在数据库里以独立的对象存储, 2.触发器不需要调用,它由一个事件来触发运行 3.触发器不能接收参数 --触发器的应用 举个例子:校内网.开心网.facebook,当你发一个日志, ...
- 网络编程1--毕向东java基础教程视频学习笔记
目录: 01 网络编程概述1 02 网络编程概述2 03网络编程 网络模型 04网络编程 IP地址 05网络编程 TCP和UDP 06网络编程 Socket 07网络编程 UDP发送端 01 网络编程 ...
- 《C#高级编程(第六版)》泛型学习笔记(一):泛型优点和特性 (转载)
原文出处:http://www.cnblogs.com/xun126/archive/2011/01/13/1933838.html 泛型是CLR 2.0的一个新特性,在CLR 1.0中,要创建一个灵 ...
- Python 中的map和reduce学习笔记
map和reduce都是Python中的内置函数 map函数接受两个参数,第一个参数是函数,第二个参数是列表,将函数依次作用于列表中的元素,并返回一个元素 reduce同样以函数和列表作为参数,区别在 ...
- 网络编程4--毕向东java基础教程视频学习笔记
Day24 06 自定义浏览器-Tomcat服务端07 自定义图形界面浏览器-Tomcat服务端08 URL-URLConnection09 小知识点10 域名解析 06 自定义浏览器-Tomcat服 ...
- 网络编程3--毕向东java基础教程视频学习笔记
Day24 01 TCP上传图片02 客户端并发上传图片03 客户端并发登录04 浏览器客户端-自定义服务端05 浏览器客户端-Tomcat服务端 01 TCP上传图片 import java.net ...
- 网络编程2--毕向东java基础教程视频学习笔记
Day 23 08 Udp接收端09 Udp键盘录入数据方式10 Udp聊天11 TCP传输12 TCP传输213 TCP练习14 TCP复制文件 08 Udp接收端 需求:定义一个应用程序,用于接收 ...
- contiki-main.c 中的process系列函数学习笔记 <contiki学习笔记之六>
说明:本文依然依赖于 contiki/platform/native/contiki-main.c 文件. ---------------------------------------------- ...
随机推荐
- 说说最易被忽略的CSS强制性换行
一般情况下,元素拥有默认的white-space:normal(自动换行,PS:不换行是white-space:nowrap),当录入的文字超过定义的宽度后会自动换行,但当录入的数据是一堆没有空格的字 ...
- Android -- 获取视频第一帧缩略图
干货 从API 8开始,新增了一个类: android.media.ThumbnailUtils这个类提供了3个静态方法一个用来获取视频第一帧得到的Bitmap,2个对图片进行缩略处理. public ...
- AI 也开源:50 大开源 AI 项目 (转)
这些开源AI项目专注于机器学习.深度学习.神经网络及其他应用场合. 自IT界早期以来,研制出能像人类那样“思考”的机器一直是研究人员的一大目标.在过去几年,计算机科学家们在人工智能(AI)领域已取得了 ...
- Java-JUC(十一):线程8锁
题目: 判断以下8种情况,输出的内容 题目一:一个Number实例对象number,两个非静态同步方法getOne,getTwo,两个线程打印输出(一个线程调用number.getOne,另外一个线程 ...
- 使用 GNU Libtool 创建库
这篇文档向大家介绍 GNU Libtool 的用途及基本使用方法,同时描述如何结合 GNU Autoconf 和 Automake 来使用 Libtool. 3 评论: 吴 小虎, 程序员, 天用唯勤 ...
- 转: wireshark过滤语法总结
from: http://blog.csdn.net/cumirror/article/details/7054496 wireshark过滤语法总结 原创 2011年12月09日 22:38:50 ...
- 转:在centos7上安装memcache
转:https://www.liquidweb.com/kb/how-to-install-memcached-on-centos-7/ http://devdocs.magento.com/guid ...
- MonoDB的数据准备
首先是数据的录入,为了分析我们服务器集群的性能,需要准备大量的用户数据,幸运的是mtools提供了mgenerate方法供我们使用.他可以根据一个数据模版向 MongoDB 中插入任意条 json ...
- [Spring Boot] @Component, @AutoWired and @Primary
Spring boot is really good for Dependencies injection by using Autowiring. Each class instancse in s ...
- C#.NET常见问题(FAQ)-如何改变字符串编码
使用Encoding.Convert方法即可实现转换 更多教学视频和资料下载,欢迎关注以下信息: 我的优酷空间: http://i.youku.com/acetaohai123 我的在线论坛: ...