(原创)mysql实例:在sql语句中使用@变量
2013-08-08 10:20
411 查看
计算用户距上次访问的天数,根据imei号区分不同的用户,如果时间段内只有一次访问则为0。
初始化数据
CREATE TABLE `pd` (
`imei` varchar(32) NOT NULL DEFAULT '',
`dat` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of pd
-- ----------------------------
INSERT INTO `pd` VALUES ('1', '2013-07-25 00:00:01');
INSERT INTO `pd` VALUES ('1', '2013-07-26 00:00:02');
INSERT INTO `pd` VALUES ('2', '2013-07-23 00:00:04');
INSERT INTO `pd` VALUES ('2', '2013-07-26 00:00:03');
INSERT INTO `pd` VALUES ('3', '2013-07-26 00:00:01');
脚本
select * from (
select imei user_id, max(max_dd) , max(max_dd_2), to_days( max(max_dd)) - to_days(max(max_dd_2)) days from (
select imei, max_dd, max_dd_2 from (
select tmp.imei, tmp.dates,
if(@imei=tmp.imei, @rank:=@rank+1,@rank:=1) as rank,
if(@rank = 1, @max_d := tmp.dates, @max_d := null) as max_dd,
if(@rank = 2, @max_d_2 := tmp.dates, @max_d_2 := null) as max_dd_2,
@imei:=tmp.imei
from (select imei, dates from pb order by imei asc ,dates desc ) tmp ,
(select @rownum :=0 , @imei := null ,@rank:=0, @max_d :=null, @max_d_2 := null) a
) result
) t
group by imei
having count(*) > 1
) x where x.days >= 1 and EXISTS (select 'x' from pb where dates > '2013-07-26 00:00:00')
如果表数据量大,千万不要用union all等操作
初始化数据
CREATE TABLE `pd` (
`imei` varchar(32) NOT NULL DEFAULT '',
`dat` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of pd
-- ----------------------------
INSERT INTO `pd` VALUES ('1', '2013-07-25 00:00:01');
INSERT INTO `pd` VALUES ('1', '2013-07-26 00:00:02');
INSERT INTO `pd` VALUES ('2', '2013-07-23 00:00:04');
INSERT INTO `pd` VALUES ('2', '2013-07-26 00:00:03');
INSERT INTO `pd` VALUES ('3', '2013-07-26 00:00:01');
脚本
select * from (
select imei user_id, max(max_dd) , max(max_dd_2), to_days( max(max_dd)) - to_days(max(max_dd_2)) days from (
select imei, max_dd, max_dd_2 from (
select tmp.imei, tmp.dates,
if(@imei=tmp.imei, @rank:=@rank+1,@rank:=1) as rank,
if(@rank = 1, @max_d := tmp.dates, @max_d := null) as max_dd,
if(@rank = 2, @max_d_2 := tmp.dates, @max_d_2 := null) as max_dd_2,
@imei:=tmp.imei
from (select imei, dates from pb order by imei asc ,dates desc ) tmp ,
(select @rownum :=0 , @imei := null ,@rank:=0, @max_d :=null, @max_d_2 := null) a
) result
) t
group by imei
having count(*) > 1
) x where x.days >= 1 and EXISTS (select 'x' from pb where dates > '2013-07-26 00:00:00')
如果表数据量大,千万不要用union all等操作
相关文章推荐
- mysql实例---sql语句中使用@变量
- 学习使用mysql需要注意的几个sql语句
- MySQL 数据库中使用 C 执行 SQL语句
- Mysql 一条SQL语句实现批量更新数据,update结合case、when和then的使用案例
- MySQL的一个一条SQL语句查询所有不同种类产品的一部分结果实例的SQL语句
- 使用SQL语句查询Mysql日志存放位置
- MySQL中使用SQL语句对字段进行重命名
- mysql sql语句执行时是否使用索引检查方法
- 使用SQL语句生成站点导航树形结构的实例
- MySQL 数据库中使用 C 执行 SQL语句
- MySQL中明知道查询结果只有一个,SQL语句中使用LIMIT 1会提高查询效率
- mysql sql优化实例1(force index使用)
- MySQL死锁问题实例分析及解决方法(主要是SQL语句可能会产生的问题)
- MySql使用show processlist查看正在执行的Sql语句
- Mysql使用入门(一)sql语句分类
- mysql 数据库Event定时任务使用详解(Navicat 及直接SQL语句创建)
- 使用C语言访问MySQL数据 —— 执行SQL语句(3)
- mysql更新图片路径(sql语句)实例
- MySQL管理之SQL语句实例 推荐
- mysql使用基础 sql语句(一)