需求
需要查询小说是否有断更,小说章节信息表中有发布时间:pub_time,如果发布时间间隔超过三天就算断更
思路
查询小说章节信息表,将章节信息按照发布时间排序,加上行号。生成table1 和table2 信息一样
left join 关联查询,table1行号为 n 与 table2行号为n+1的数据发布时间比较,如果存在大于三天则说明断更
准备工作
章节表:
create table `t_chapter` ( `id` varchar(255) not null comment '主键', `auto_code` varchar(255) not null comment '编号', `production_number` varchar(11) not null comment '作品编号', `pub_time` datetime default null comment '发布时间', primary key (`id`)) engine=innodb default charset=utf8
开始
对章节表根据发布时间升序排序并显示行号
select t.auto_code , t.id , t.production_number , t.pub_time , (@rownum :=@rownum + 1) as rowno from t_chapter t , (select(@rownum := 0)) b where t.production_number = 1414(指定作品) order by t.pub_time asc
查询结果已经按照发布时间排序
关联查询
select count(1) from ( select t.auto_code , t.id , t.production_number , t.pub_time , (@rownum :=@rownum + 1) as rowno from t_chapter t , (select(@rownum := 0)) b where t.production_number = 979 order by t.pub_time asc ) table1 inner join( select t.auto_code , t.id , t.production_number , t.pub_time , (@a :=@a + 1) as rowno from t_chapter t , (select(@a := 0)) b where t.production_number = 979 order by t.pub_time asc ) table2 on table1.rowno + 1 = table2.rowno where timestampdiff(day , table2.pub_time , table1.pub_time) > 3;
如果查询count>0则作品编号为979的作品存在断更,更多条件可以依据自己业务而定
说明:
这里用到了@,最开始我也不知道这是什么东西,然后我搜索mysql行号查到使用自定义变量(mysql特性)进行排序显示
参考博客:
sql server查询行号
mysql rownum 实现
mysql 自定义变量使用(推荐)
需求
需要查询小说是否有断更,小说章节信息表中有发布时间:pub_time,如果发布时间间隔超过三天就算断更
思路
查询小说章节信息表,将章节信息按照发布时间排序,加上行号。生成table1 和table2 信息一样
left join 关联查询,table1行号为 n 与 table2行号为n+1的数据发布时间比较,如果存在大于三天则说明断更
准备工作
章节表:
create table `t_chapter` ( `id` varchar(255) not null comment '主键', `auto_code` varchar(255) not null comment '编号', `production_number` varchar(11) not null comment '作品编号', `pub_time` datetime default null comment '发布时间', primary key (`id`)) engine=innodb default charset=utf8
开始
对章节表根据发布时间升序排序并显示行号
select t.auto_code , t.id , t.production_number , t.pub_time , (@rownum :=@rownum + 1) as rowno from t_chapter t , (select(@rownum := 0)) b where t.production_number = 1414(指定作品) order by t.pub_time asc
查询结果已经按照发布时间排序
关联查询
select count(1) from ( select t.auto_code , t.id , t.production_number , t.pub_time , (@rownum :=@rownum + 1) as rowno from t_chapter t , (select(@rownum := 0)) b where t.production_number = 979 order by t.pub_time asc ) table1 inner join( select t.auto_code , t.id , t.production_number , t.pub_time , (@a :=@a + 1) as rowno from t_chapter t , (select(@a := 0)) b where t.production_number = 979 order by t.pub_time asc ) table2 on table1.rowno + 1 = table2.rowno where timestampdiff(day , table2.pub_time , table1.pub_time) > 3;
如果查询count>0则作品编号为979的作品存在断更,更多条件可以依据自己业务而定
说明:
这里用到了@,最开始我也不知道这是什么东西,然后我搜索mysql行号查到使用自定义变量(mysql特性)进行排序显示
相关文章:
c语言中冒泡排序、插入排序、选择排序算法比较
插入排序 排序算法学习-插入排序
相关视频:
数据库mysql视频教程
以上就是mysql显示行号排序、同张表数据排序上下进行比较的详细内容。