关于分组序号在MySQL中的实现
生活随笔
收集整理的這篇文章主要介紹了
关于分组序号在MySQL中的实现
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
好像ORACLE中有相應的函數(shù),可惜在MSSQL 或者MySQL中沒有對應的函數(shù)。后兩者就得用臨時表來實現(xiàn)了。
create table company
(dep char(10) not null,
val1 int unsigned not null
);
insert into company values
('市場部', 26),
('市場部',25),
('市場部',24),
('辦公室',16),
('辦公室',12),
('研發(fā)部',19),
('研發(fā)部',11);
1)、循環(huán)實現(xiàn)
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)、游標實現(xiàn)
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();
(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)
create table company
(dep char(10) not null,
val1 int unsigned not null
);
insert into company values
('市場部', 26),
('市場部',25),
('市場部',24),
('辦公室',16),
('辦公室',12),
('研發(fā)部',19),
('研發(fā)部',11);
1)、循環(huán)實現(xiàn)
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)、游標實現(xiàn)
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 |
| 研發(fā)部 | 19 | 1 |
| 研發(fā)部 | 11 | 2 |
uery result(7 records)
| dep | val1 | num |
| 辦公室 | 16 | 1 |
| 辦公室 | 12 | 2 |
| 市場部 | 26 | 1 |
| 市場部 | 25 | 2 |
| 市場部 | 24 | 3 |
| 研發(fā)部 | 19 | 1 |
| 研發(fā)部 | 11 | 2 |
(15 ms taken)
(0 row(s)affected)
(0 ms taken)
(7 row(s)returned)
(16 ms taken)
(0 row(s)affected)
(0 ms taken)
轉載于:https://www.cnblogs.com/secbook/archive/2008/07/30/2655295.html
總結
以上是生活随笔為你收集整理的关于分组序号在MySQL中的实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql双机热备份的实现步骤
- 下一篇: Hamilton Verissimo的b