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 文件. ---------------------------------------------- ...
随机推荐
- windows下mysql忘记root密码的解决办法
今天早上 一朋友说自己的mysql 忘记root密码了 让我帮忙给看看,因为没有接触过mysql 所以从网上找了一下信息经我亲身实践 已经成功!mysql版本是5.1以下是从网上找的信息: 1. 首 ...
- 中文分词器ICTCLAS使用方法(Java)
http://www.cnblogs.com/CheeseZH/archive/2012/11/27/2791037.html 吃水不忘挖井人,这篇文章给了我很大帮助:http://blog.csdn ...
- logistic回归(一)
http://www.2cto.com/kf/201307/226576.html , 这个是Sigmoid函数,在这个回归过程中非常重要的函数,主要的算法思想和这个密切相关.这个函数的性质大家可以自 ...
- jquery validation对隐藏的元素不进行验证
validation默认不会对Hidden元素进行验证的,但最近使用了thinkcmf开发了一个系统后台,在验证时发现隐藏的元素也进行了验证 刚开始以为是 validation版本问题(当前版本取消了 ...
- Eclipse关掉项目SVN的链接
有时候 svn 会导致 eclipse 反应很慢,可以关掉 svn项目信息展现. 1. 点击项目文件夹,右键出现项目信息 2. 选择team项 3. Disconnect.
- PostBuildEvent
<PostBuildEvent>CALL "%25VS90COMNTOOLS%25\vsvars32.bat" > NULL sn –Vr $(TargetFil ...
- php manager
https://phpmanager.codeplex.com/releases/view/69115
- 关于Linux路由表的route命令
转自:http://www.cnblogs.com/gunl/archive/2010/09/14/1826234.html 查看 Linux 内核路由表 使用下面的 route 命令可以查看 Lin ...
- OpenStack云计算之路-Mitaka 版本
1.1 云计算简介 云计算(英语:cloud computing ),是一种基于互联网的计算方式,通过这种方式,共享的软硬件资源和信息可以按需求提供给计算机各种终端和其他设备. 云计算是继1980年代 ...
- Try Before Choosing
 Try Before Choosing Erik Doernenburg CREATing An AppliCATion REquiRES MAKing MAny dECiSionS. Some ...