在mysql中進行統計排序(跳躍排序和連續排序)


-- 在mysql中進行統計排序(跳躍排序和連續排序)
CREATE TABLE `tb_score` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`score` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id_UNIQUE` (`id`)
)

select * from tb_score;

delete from tb_score where id > 0;
commit;

insert into tb_score (score) values(43),(22),(22),(18);
commit;

-- 跳躍排序,有並列名次的情況下,整個排序序號不連續
-- 將原表添加一列自增序號的列,然后按照數字進行分組,取得最小的自增序號,在最外層按照各自的最小序號排序
select z.*, x.min_cnt from tb_score z,
(
select score, min(cnt) min_cnt from (
(select id, score, (@i:=@i+1) cnt from tb_score, (SELECT @i:=0) as i order by score ) ) a
group by score ) x
where z.score = x.score
order by x.min_cnt;

-- 連續排序,有並列名次的情況下,整個排序序號是連續
-- 將原表對數字進行分組並排序,然后將其做為一個子表,在上面添加一個自增的序號
select * from tb_score t , (
select tmp.score, (@i:=@i+1) rank from (
select score from tb_score
group by score order by score asc ) tmp,(SELECT @i:=0) as i ) b
where t.score = b.score
order by b.rank;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM