关于分组序号在MySQL中的实现

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,高可用系列 2核4GB
简介:
好像ORACLE中有相应的函数,可惜在MSSQL 或者MySQL中没有对应的函数。后两者就得用临时表来实现了。

create table company
(dep char(10) not null,
val1 int unsigned not null
);
insert into company values
(
'市场部', 26),
('市场部',25),
('市场部',24),
('办公室',16),
('办公室',12),
('研发部',19),
('研发部'
,11);

1)、循环实现

DELIMITER $$

CREATE DEFINER=`root`@`%` PROCEDURE `sp_generate_auto`()
BEGIN
declare cnt int default 0;
declare i int default 0;
drop table if exists tmp;
-- Temporary table to save the result.
create temporary table tmp like company;
alter table tmp add num int unsigned not null;
select count(1) as total from (select count(1) from company where 1 group by dep) T into cnt;
while i < cnt
do
set @stmt = concat('select dep from company where 1 group by dep order by dep asc limit ',i,',1 into @t_dep');
prepare s1 from @stmt;
execute s1;
deallocate prepare s1;
set @stmt = NULL;
set @num = 0;
set @stmt2 = concat('insert into tmp select dep,val1,@num := @num + 1 as sequence from company where dep = ''',@t_dep,''' order by dep asc');
prepare s1 from @stmt2;
execute s1;
deallocate prepare s1;
set @stmt2 = NULL;
set i = i + 1;
end while;
select * from tmp;
set @t_dep = NULL;
END$$

DELIMITER ;

2)、游标实现

DELIMITER $$



DROP PROCEDURE IF EXISTS `sp_generate_auto_cursor`$$



CREATE DEFINER=`root`@`%` PROCEDURE `sp_generate_auto_cursor`()

BEGIN

  declare done1 int default 0;

  declare a char(10);

  declare i int unsigned default 0;
-- Cursor one to get the group total
  declare cur1 cursor for select dep from company group by dep;

  declare continue handler for 1329 set done1 = 1;

-- Temporary table to save the result.

  drop table if exists tmp;

  create table tmp like company;

  alter table tmp add num int unsigned not null;

  open cur1;

  while done1 != 1

  do

    fetch cur1 into a;

    if not done1 then

      set @i = 0;

      begin

      declare done2 int default 0;

      declare b int unsigned default 0;

      declare c int unsigned default 0;
-- Cursor two to get per group total.
      declare cur2 cursor for select val1,@i := @i + 1 from company where dep = a;

      declare continue handler for 1329 set done2 = 1;

        open cur2;

        while done2 <> 1

        do

          fetch cur2 into b,c;

          if not done2 then

            insert into tmp select a,b,c;

          end if;

        end while;

        close cur2;

      end;

    end if;

  end while;

  close cur1;

  select * from tmp;

END$$



DELIMITER ;





call sp_generate_auto();
call sp_generate_auto_cursor();

query result(7 records)

dep val1 num
办公室 16 1
办公室 12 2
市场部 26 1
市场部 25 2
市场部 24 3
研发部 19 1
研发部 11 2

uery result(7 records)

dep val1 num
办公室 16 1
办公室 12 2
市场部 26 1
市场部 25 2
市场部 24 3
研发部 19 1
研发部 11 2
(7 row(s)returned)
(15 ms taken)

(0 row(s)affected)
(0 ms taken)

(7 row(s)returned)
(16 ms taken)

(0 row(s)affected)

(0 ms taken)





本文转自 yarin 51CTO博客,原文链接:http://blog.51cto.com/yueliangdao0608/85145,如需转载请自行联系原作者

相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
5天前
|
算法 关系型数据库 MySQL
MySQL高级篇——排序、分组、分页优化
排序优化建议、案例验证、范围查询时索引字段选择、filesort调优、双路排序和单路排序、分组优化、带排序的深分页优化
MySQL高级篇——排序、分组、分页优化
|
1月前
|
SQL 关系型数据库 MySQL
MySQL】-DQL(基本、条件、分组、排序、分页)详细版
通过这些查询方法,你可以高效地检索、分析和组织MySQL数据库中的数据,以满足各种应用需求。实践中,理解这些SQL语句的基础知识以及它们如何组合起来进行复杂的数据操作是至关重要的。
26 1
|
2月前
|
SQL 关系型数据库 MySQL
MySQL获取分组里的最新数据如何写sql
MySQL获取分组里的最新数据如何写sql
33 0
|
3月前
|
关系型数据库 MySQL
10. Mysql 分组或汇总查询
10. Mysql 分组或汇总查询
31 1
|
4月前
|
SQL 关系型数据库 MySQL
简简单单 My SQL 学习笔记(2)——分组和简单数据的查询
简简单单 My SQL 学习笔记(2)——分组和简单数据的查询
|
3月前
|
关系型数据库 MySQL 数据库
MySQL的排序、分组、合并
MySQL的排序、分组、合并
|
3月前
|
SQL 关系型数据库 MySQL
MySQL分组查询以及having筛选
MySQL分组查询以及having筛选
23 0
|
3月前
|
SQL 关系型数据库 MySQL
MySQL多表联合查询+分组+排序
MySQL多表联合查询+分组+排序
27 0
|
3月前
|
SQL 关系型数据库 MySQL
MySQL分组查询实例
MySQL分组查询实例
27 0
|
3月前
|
SQL 关系型数据库 MySQL
MySQL数据库——SQL(3)-DQL(基本查询、条件查询、聚合函数、分组查询、排序查询、分页查询、案例练习)
MySQL数据库——SQL(3)-DQL(基本查询、条件查询、聚合函数、分组查询、排序查询、分页查询、案例练习)
48 0

热门文章

最新文章