SQL开发知识:mysql如何查找连续出现n次以上的数
查找连续出现n次以上的数
id连续的,因此三表关联查询
select distinct(l1.num) as ConsecutiveNums
from Logs l1,Logs l2,Logs l3
where
l1.id=l2.id-1 and l1.id=l3.id-2
and l1.num=l2.num and l1.num =l3.num
from Logs l1,Logs l2,Logs l3
where
l1.id=l2.id-1 and l1.id=l3.id-2
and l1.num=l2.num and l1.num =l3.num
方法二,利用自定义变量进行分组 ,将连续的值都分到一个组里面,然后就可以group by 分组找出来了 。
select distinct(num) as ConsecutiveNums from
(
select id,num,
if(@lastNum!=num,@group:=@group+1,@group) as g,
if(@lastNum!=num,@lastNum:=num,@lastNum) as c from
(select id,num from Logs )a,
(select @lastNum:=0,@group:=0 )b
) m group by g having count(1)>=3 order by id
(
select id,num,
if(@lastNum!=num,@group:=@group+1,@group) as g,
if(@lastNum!=num,@lastNum:=num,@lastNum) as c from
(select id,num from Logs )a,
(select @lastNum:=0,@group:=0 )b
) m group by g having count(1)>=3 order by id
查询重复出现次数最多的记录
MySQL查询的方法很多,下面为您介绍的MySQL查询语句用于实现查询重复出现次数最多的记录,对于学习MySQL查询有很好的帮助作用。
在有些应用里面,我们需要查询重复次数最多的一些记录,虽然这是一个很简单的查询语句,但是对许多初学者来说,仍然有些难度,特发此文章备查。
SELECT keyword, count( * ) AS count
FROM article_keyword
GROUP BY keyword
ORDER BY count DESC
LIMIT 20
FROM article_keyword
GROUP BY keyword
ORDER BY count DESC
LIMIT 20
此段查询语句返回 article_keyword 表中 keyword 重复次数(count)最多的20条记录。
SELECT DISTINCT count( * ) AS count
FROM article_keyword
GROUP BY keyword
ORDER BY count DESC
LIMIT 6
FROM article_keyword
GROUP BY keyword
ORDER BY count DESC
LIMIT 6
此段查询语句返回 article_keyword 表中 keyword 的重复次数(count)排名前 6 的数值。通过添加 DISTINCT 返回唯一记录。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。