千家信息网

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

发表于:2024-09-22 作者:千家信息网编辑
千家信息网最后更新 2024年09月22日,这篇文章给大家分享的是有关mysql中如何查询前50%的数据的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。实验环境:create table t (c int);inser
千家信息网最后更新 2024年09月22日mysql中如何查询前50%的数据

这篇文章给大家分享的是有关mysql中如何查询前50%的数据的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

实验环境:

create table t (c int);
insert into t values
(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信息。

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


  1. select t5.*,t6.*,ifnull(round(t5.sumtotal/t6.total,2),0) result from (

  2. select t3.rn,t3.total,t3.c,sum(ifnull(t4.total,0)) sumtotal from (

  3. select

  4. t1.*,

  5. (

  6. select count(case when t2.total>t1.total then 1 when t2.total=t1.total and t2.c

  7. select c,count(*) total from t group by c

  8. ) t2

  9. ) rn

  10. from(

  11. select c,count(*) total from t group by c order by total desc ,c

  12. ) t1

  13. ) t3

  14. left join(

  15. select

  16. t1.*,

  17. (

  18. select count(case when t2.total>t1.total then 1 when t2.total=t1.total and t2.c

  19. select c,count(*) total from t group by c

  20. ) t2

  21. ) rn

  22. from(

  23. select c,count(*) total from t group by c order by total desc ,c

  24. ) t1

  25. ) t4 on (t3.rn> t4.rn)

  26. group by t3.rn,t3.total,t3.c

  27. ) t5

  28. left join(

  29. select count(*) total from t

  30. ) t6 on(1=1)

  31. where ifnull(round(t5.sumtotal/t6.total,2),0)<=0.5;

查询结果:


绑定MySQL的实现

  1. select * from (

  2. select t3.*,case when result<=0.5 then 1 when result>0.5 and @b=-1 then @b:=1 else null end r from (

  3. select t1.*,round((@a:=@a+t1.total)/t2.total,2) result from (

  4. select c,count(*) total from t,(select @a:=0,@b:=-1) vars group by c order by 2 desc

  5. ) t1

  6. left join(

  7. select count(*) total from t

  8. ) t2 on(1=1)

  9. ) t3 order by result

  10. ) t4 where r=1;


结果:


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

数据 查询 内容 更多 篇文章 结果 不错 实用 信息 数据库 数量 文章 方式 环境 看吧 知识 参考 实验 帮助 有关 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 杭州网络安全优选柚米 网络安全龙头低估值 科技互联网宣传主标语 软件开发的速度 数据库的索引与作用 论述网络安全等级保护的意义 重庆软件开发岗位薪资 浙江正规网络技术哪家便宜 计算机网络技术课程免费 sim卡无效激活服务器现有 软件开发及系统集成的英文 分析网络安全协议 wifi服务器地址怎么改 邯郸网络安全工程师 计算机平面设计及网络技术 青海优质软件开发价格 网络安全志愿宣传总结 连接数据库 找不到网络路径 天地心网络技术怎么样 服务器机柜安装螺丝哪里有卖的 用条码机打印考号数据库变量为空 数据库原理dfd是什么 浙江正规网络技术哪家便宜 河北捡乐网络技术有限公司简介 安徽综合软件开发厂家报价 手机远程服务器怎么登陆 互联网科技税收优惠 新入行员工网络安全培训 黑龙江数据库安全箱代理价钱 软件开发专业前途如何
0