In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how MySQL uses variables to achieve a variety of sorting, has a certain reference value, friends in need can refer to. I hope you will learn a lot after reading this article. Next, let the editor take you to learn about it.
Core code
-- next I'll demonstrate the implementation of sorting in MySQL-- Test data CREATE TABLE tb (score INT); INSERT tb SELECT 5 UNION ALL SELECT 4 UNION ALL SELECT 4 UNION ALL SELECT 4 UNION ALL SELECT 3 UNION ALL SELECT 2 UNION ALL SELECT1;--1.row_number sort SET @ row_number = 0 position select @ row_number: = @ row_number+1 AS row_number,score FROM tb ORDER BY score DESC +-+-+ | row_number | score | +-+-+ | 1 | 5 | 2 | 4 | 3 | 4 | 4 | 4 | 5 | 3 | 6 | 2 | 7 | 1 | +- -2.dense_rank sort SET @ dense_rank = 0 @ prev_score = NULL SELECT @ dense_rank: = IF (@ prev_score=score,@dense_rank,@dense_rank+1) AS decnse_rank, @ prev_score: = score AS score FROM tb ORDER BY score DESC +-+-+ | decnse_rank | score | +-+-+ | 1 | 5 | 2 | 4 | 2 | 4 | 2 | 4 | 3 | 3 | 4 | 4 | 5 | 1 | +- Sort SET @ row=0 of-+-- 3.rank style @ rank=0,@prev_score=NULL SELECT @ row:=@row+1 AS ROW, @ rank:=IF (@ prev_score=score,@rank,@row) AS rank,@ prev_score:=score AS scoreFROM tb ORDER BY score DESC +-+ | ROW | rank | score | +-+ | 1 | 1 | 5 | 2 | 2 | 4 | 3 | 2 | 4 | 4 | 2 | 4 | 5 | 5 | 6 | 6 | 2 | 2 | 7 | 7 | 1 | +- -+ Thank you for reading this article carefully. I hope it is helpful for everyone to share how MySQL uses variables to achieve a variety of ranking content. At the same time, I also hope that you will support it, pay attention to the industry information channel, and find out if you encounter problems. Detailed solutions are waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 240
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.