bl双性强迫侵犯h_国产在线观看人成激情视频_蜜芽188_被诱拐的少孩全彩啪啪漫画

mysql中如何查詢前50%的數據

這篇文章給大家分享的是有關MySQL中如何查詢前50%的數據的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

創新互聯建站2013年至今,先為白銀等服務建站,白銀等地企業,進行企業商務咨詢服務。為白銀企業網站制作PC+手機+微官網三網同步一站式服務解決您的所有建站問題。

實驗環境:

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; 


mysql中如何查詢前50%的數據
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<t1.c then 1 else null end)+1 from (

  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<t1.c then 1 else null end)+1 from (

  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中如何查詢前50%的數據

綁定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%的數據

感謝各位的閱讀!關于“mysql中如何查詢前50%的數據”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

新聞標題:mysql中如何查詢前50%的數據
文章出自:http://vcdvsql.cn/article28/gjojjp.html

成都網站建設公司_創新互聯,為您提供ChatGPT做網站自適應網站搜索引擎優化網站排名關鍵詞優化

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

網站托管運營