当前位置:网站首页>【mysql】mysql中行排序

【mysql】mysql中行排序

2022-07-06 20:50:00 檀越剑指大厂

mysql中行排序

1.数据准备

drop table if exists kwan.tmp_learning_mary;
create table if not exists kwan.tmp_learning_mary(
    id varchar(10)
    , name varchar(10) 
    ,age varchar(10)
    , salary int 
);
insert into kwan.tmp_learning_mary(id, name, age, salary)
values (1, 'a', 10, 8000);
insert into kwan.tmp_learning_mary(id, name, age, salary)
values (1, 'a2', 11, 6500);
insert into kwan.tmp_learning_mary(id, name, age, salary)
values (2, 'b', 12, 13000);
insert into kwan.tmp_learning_mary(id, name, age, salary)
values (2, 'b2', 13, 4500);
insert into kwan.tmp_learning_mary(id, name, age, salary)
values (3, 'c', 14, 3000);
insert into kwan.tmp_learning_mary(id, name, age, salary)
values (3, 'c2', 15, 20000);
insert into kwan.tmp_learning_mary(id, name, age, salary)
values (4, 'd', 16, 30000);
insert into kwan.tmp_learning_mary(id, name, age, salary)
values (5, 'd2', 17, 1800);

2.编写sql加条件

select
	*
from
	(
	select
		*,
		row_number() over(partition by id
	order by
		salary desc) ranking
	from
		tmp_learning_mary) t
where
	t.ranking < 2;

image-20220706112759789

3.编写sql不加条件

select
	*
from
	(
	select
		*,
		row_number() over(partition by id
	order by
		salary desc) ranking
	from
		tmp_learning_mary) t

image-20220706112831790

原网站

版权声明
本文为[檀越剑指大厂]所创,转载请带上原文链接,感谢
https://qinyingjie.blog.csdn.net/article/details/125636388