mysql中如何查询前50%的数据


这篇文章给大家分享的是有关mysql中如何查询前50%的数据的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。 实验环境:
createtablet(cint);
insertintotvalues
(15),(7),(9),(10),(7),(8),(20),(16),(9),(19),
(14),(10),(11),(10),(10),(12),(7),(10),(7),(9);
commit;

c 等于10,7,9的数量有 11个,超过了半数。
超过半数的 item信息。

通用方式,不依赖具体数据库。

select t5.*,t6.*,ifnull(round(t5.sumtotal/t6.total,2),0) result from (
select t3.rn,t3.total,t3.c,sum(ifnull(t4.total,0)) sumtotal from (
select
t1.*,
(
select count(case when t2.total>t1.total then 1 when t2.total=t1.total and t2.c
select c,count(*) total from t group by c
) t2
) rn
from(
select c,count(*) total from t group by c order by total desc ,c
) t1
) t3
left join(
select
t1.*,
(
select count(case when t2.total>t1.total then 1 开发云主机域名when t2.total=t1.total and t2.c
select c,count(*) total from t group by c
) t2
) rn
from(
select c,count(*) total from t group by c order by total desc ,c
) t1
) t4 on (t3.rn> t4.rn)
group by t3.rn,t3.total,t3.c
) t5
left join(
select count(*) total from t
) t6 on(1=1)
where ifnull(round(t5.sumtotal/t6.total,2),0)查询结果:

绑定MySQL的实现

select * from (
select t3.*,case when result0.5 and @b=-1 then @b:=1 else null end r from (
select t1.*,round((@a:=@a+t1.total)/t2.total,2) result from (
select c,count(*) total from t,(select @a:=0,@b:=-1) vars group by c order by 2 desc
) t1
left join(
select count(*) total from t
) t2 on(1=1)
) t3 order by result
) t4 where r=1;
结果:

感谢各位的阅读!关于“mysql中如何查询前50%的数据”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

相关推荐: wdCP面板MySQL版本怎么升级

这篇文章给大家分享的是有关wdCP面板MySQL版本怎么升级的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。之前安装wdCP面板主要就是为了搭建WordPress个人博客,然后去WordPress官网一看,发现系统要求的是PHP7.3…

免责声明:本站发布的图片视频文字,以转载和分享为主,文章观点不代表本站立场,本站不承担相关法律责任;如果涉及侵权请联系邮箱:360163164@qq.com举报,并提供相关证据,经查实将立刻删除涉嫌侵权内容。

(0)
打赏 微信扫一扫 微信扫一扫
上一篇 06/22 17:36
下一篇 06/22 17:36

相关推荐