日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > 数据库 >内容正文

数据库

mysql查询连续3个月以上_MySQL查询显示连续的结果

發(fā)布時(shí)間:2023/12/10 数据库 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql查询连续3个月以上_MySQL查询显示连续的结果 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

#mysql中 對(duì)于查詢結(jié)果只顯示n條連續(xù)行的問(wèn)題#

在領(lǐng)扣上碰到的一個(gè)題目:求滿足條件的連續(xù)3行結(jié)果的顯示

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.首先先進(jìn)行結(jié)果集的查詢

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

2.給查詢的結(jié)果集增加一個(gè)自增列

select @newid:=@newid+1 as newid,test.*

from(select @newid:=0)r, test where people>100

3.自增列和id的差值 相同即連續(xù)

select @newid:=@newid+1 as newid,test.* ,@cha:=id-@newid as cha

from(select @newid:=0)r, test where people>100

4.將相同的差值 放在同一張表中,并取出連續(xù)數(shù)量大于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);

聽(tīng)說(shuō)還可以用存儲(chǔ)過(guò)程來(lái)完成,不過(guò)我沒(méi)嘗試,稍后嘗試

以上

如您對(duì)本文有疑問(wèn)或者有任何想說(shuō)的,請(qǐng)點(diǎn)擊進(jìn)行留言回復(fù),萬(wàn)千網(wǎng)友為您解惑!

總結(jié)

以上是生活随笔為你收集整理的mysql查询连续3个月以上_MySQL查询显示连续的结果的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。