mysql中 对于查询结果只显示n条连续行的问题#


在领扣上碰到的一个题目:求满足条件的连续3行结果的显示

X city built a new stadium, each day many people visit it and the stats are saved as these columns: id, date, people;
Please write a query to display the records which have 3 or more consecutive rows and the amount of people more than 100(inclusive).
For example, the table stadium:
+------+------------+-----------+
| id | date | people |
+------+------------+-----------+
| 1 | 2017-01-01 | 10 |
| 2 | 2017-01-02 | 109 |
| 3 | 2017-01-03 | 150 |
| 4 | 2017-01-04 | 99 |
| 5 | 2017-01-05 | 145 |
| 6 | 2017-01-06 | 1455 |
| 7 | 2017-01-07 | 199 |
| 8 | 2017-01-08 | 188 |
+------+------------+-----------+ For the sample data above, the output is:
+------+------------+-----------+
| id | date | people |
+------+------------+-----------+
| 5 | 2017-01-05 | 145 |
| 6 | 2017-01-06 | 1455 |
| 7 | 2017-01-07 | 199 |
| 8 | 2017-01-08 | 188 |
+------+------------+-----------+

1.首先先进行结果集的查询

select id,date,people from stadium where people>=100;

2.给查询的结果集增加一个自增列

SELECT @newid:=@newid+1 AS newid,test.*
FROM(SELECT @newid:=0)r, test WHERE people>100

3.自增列和id的差值 相同即连续

SELECT @newid:=@newid+1 AS newid,test.* ,@cha:=id-@newid AS cha
FROM(SELECT @newid:=0)r, test WHERE people>100

4.将相同的差值 放在同一张表中,并取出连续数量大于3的

select if(count(id)>=3,count_concat(id),null)e from(
SELECT @newid:=@newid+1 AS newid,test.* ,@cha:=id-@newid AS cha
FROM(SELECT @newid:=0)r, test WHERE people>100)
as d group by cha

5.将上步得到的表和主表 取得所需要的

SELECT id,DATE,people FROM test,
(SELECT IF (COUNT(id)>3,GROUP_CONCAT(id),NULL)e
FROM (SELECT @newid:=@newid+1 AS newid,test.* ,@cha:=id-@newid AS cha
FROM(SELECT @newid:=0)r, test WHERE people>100)AS d GROUP BY cha ) AS f
WHERE f.e IS NOT NULL AND FIND_IN_SET(id,f.e);

听说还可以用存储过程来完成,不过我没尝试,稍后尝试

以上

MySQL查询显示连续的结果的更多相关文章

  1. mysql查询之 连续出现的数字,重复出现的邮箱,删除重复的电子邮箱

    1.编写一个 SQL 查询,查找所有至少连续出现三次的数字. +----+-----+ | Id | Num | +----+-----+ | 1 | 1 | | 2 | 1 | | 3 | 1 | ...

  2. MySQL查询in操作 查询结果按in集合顺序显示(转)

    MySQL 查询in操作,查询结果按in集合顺序显示的实现代码,需要的朋友可以参考下. MySQL 查询in操作,查询结果按in集合顺序显示 复制代码代码如下: select * from test ...

  3. MySQL查询in操作 查询结果按in集合顺序显示_Mysql_脚本之家

    body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...

  4. MySQL 查询in操作,查询结果按in集合顺序显示

    MySQL 查询in操作,查询结果按in集合顺序显示   select * from test where id in(3,1,5) order by find_in_set(id,'3,1,5'); ...

  5. 命令行下更好显示 mysql 查询结果

    在 linux命令行中,直接进行 mysql查询时,有时查询的结果字段较多,显示的效果就很不友好: 但 mysql支持以另一种方式来显示结果,如下: 普通是 SQL 是以分号 ; 结束的,如果改为 \ ...

  6. mysql 查询结果显示行号

    mysql 查询时,不像oracle那样,可以直接用 rownum 显示结果行号. 可以用定义用户变量来实现 set @myrnum = 0; select (@myrnum := @myrnum + ...

  7. MySql查询进阶

    1.1 as关键字 用于 给显示结果中字段 或者 表 起别名 select 别名.字段名 from 表名 as 别名 where 条件语句 # 对字段起别名 select id as '编号', na ...

  8. Mysql 查询练习

    Mysql 查询练习 ---创建班级表 create table class( cid int auto_increment primary key, caption ) )engine=innodb ...

  9. 【转】mysql查询结果输出到文件

    转自:http://www.cnblogs.com/emanlee/p/4233602.html mysql查询结果导出/输出/写入到文件 方法一: 直接执行命令: mysql> select ...

随机推荐

  1. js 阻止元素发生默认的行为ie兼容

    //亲测ie9可以兼容 function Go(event) { var e = event; if (e && e.preventDefault) { e.preventDefaul ...

  2. gedit配置

    编辑 \(\rightarrow\) 首选项 \(\rightarrow\) 插件 \(\rightarrow\) 外部工具 启用 进入工具 \(\rightarrow\) Manage Extern ...

  3. 题解 P1854 花店橱窗布置

    把二维压成一维的DP了解一下... 传送门 (以纪念神经兮兮调了两天的一维DP(刷水题谋财害命)以及感谢学长的帮助@ydnhaha) 显然我们有二维的dp:f[i][j]代表第i盆花放到第j个位置 ; ...

  4. bearBaby loves sleeping(BFS)

    时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其他语言262144K 64bit IO Format: %lld 题目描述 Sleeping is a favorit ...

  5. 洛谷P2664 树上游戏

    https://www.luogu.org/problemnew/show/P2664 #include<cstdio> #include<algorithm> #includ ...

  6. JavaScript 中Array数组的几个内置函数

    本文章内容均参考<JavaScript高级程序设计第三版> 今天在看JavaScript书籍的时候,看到之前没有了解过的JavaScript中Array的几个内置函数对象,为了之后再开发工 ...

  7. Proto.Actor模型

    Proto.Actor模型 http://proto.actor/ https://github.com/axzxs2001/ProtoActorSample https://www.cnblogs. ...

  8. python 8 函数

    调用函数 Python内置了很多有用的函数,我们可以直接调用. 要调用一个函数,需要知道函数的名称和参数,比如求绝对值的函数abs,只有一个参数.可以直接从Python的官方网站查看文档: 也可以在交 ...

  9. java jstat

    jstat 虚拟机统计信息监视工具: jstat (JVM Statistics Monitoring Tool) 适用于监视虚拟机各种运行状态信息的命令行工具. 命令格式: jstat [ opti ...

  10. SQL server下所有表名及字段名及注释查询

    --查询所有表及注释SELECTA.name ,C.valueFROM sys.tables A LEFT JOIN sys.extended_properties C ON C.major_id = ...