這篇文章給大家分享的是有關(guān)MySQL中如何查詢前50%的數(shù)據(jù)的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
創(chuàng)新互聯(lián)建站2013年至今,先為白銀等服務(wù)建站,白銀等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為白銀企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。
實(shí)驗(yàn)環(huán)境:
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的數(shù)量有 11個(gè),超過(guò)了半數(shù)。
超過(guò)半數(shù)的 item信息。
通用方式,不依賴具體數(shù)據(jù)庫(kù)。
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<t1.c then 1 else null end)+1 from (
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<t1.c then 1 else null end)+1 from (
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)<=0.5;
查詢結(jié)果:
綁定MySQL的實(shí)現(xiàn)
select * from (
select t3.*,case when result<=0.5 then 1 when result>0.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;
結(jié)果:
感謝各位的閱讀!關(guān)于“mysql中如何查詢前50%的數(shù)據(jù)”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
新聞標(biāo)題:mysql中如何查詢前50%的數(shù)據(jù)
文章出自:http://aaarwkj.com/article28/gjojjp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、做網(wǎng)站、自適應(yīng)網(wǎng)站、搜索引擎優(yōu)化、網(wǎng)站排名、關(guān)鍵詞優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)