首页 > 代码库 > Mysql间隔取数据,实现sqlserver的row_number()函数
Mysql间隔取数据,实现sqlserver的row_number()函数
1. 使用临时表
CREATE DEFINER=`root`@`%` PROCEDURE `sp_getMonitorInfo`(IN d_itemId INT, IN d_configId INT, d_count_num INT )
begin
set @count = 0;
set @num = 0;
SELECT @count :=count(1) FROM better.MonitorInfo where itemId=d_itemId and configId=d_configId;
IF @count<300
THEN
SELECT id,cpu,cpu1,cpu2,cpu3,diskRead,diskWrite,memory,networkReceive,networkSend,time,configId,itemId FROM MonitorInfo where itemId=itemId and configId=configId;
ELSE
SET @num= round(@count/d_count_num,0);
select @num;
create temporary table tmp_MonitorInfo
(
tmp_id int(4) primary key not null auto_increment,
id int(4) not null,
cpu int,
cpu1 int,
cpu2 int,
cpu3 int,
diskRead int,
diskWrite int,
memory int,
networkReceive int,
networkSend int,
time varchar(40),
configId int,
itemId int
);
insert into tmp_MonitorInfo(id,cpu,cpu1,cpu2,cpu3,diskRead,diskWrite,memory,networkReceive,networkSend,time,configId,itemId)
select id,cpu,cpu1,cpu2,cpu3,diskRead,diskWrite,memory,networkReceive,networkSend,time,configId,itemId from MonitorInfo
where itemId=d_itemId and configId=d_configId;
select id,cpu,cpu1,cpu2,cpu3,diskRead,diskWrite,memory,networkReceive,networkSend,time,configId,itemId from tmp_MonitorInfo
where tmp_id%@num=0;
drop table tmp_MonitorInfo;
END IF;
end
2. 使用临时变量
CREATE DEFINER=`root`@`%` PROCEDURE `sp_getMonitorInfo_2`(IN d_itemId INT, IN d_configId INT, d_count_num INT )
begin
set @count = 0;
set @num = 0;
SELECT @count :=count(1) FROM better.MonitorInfo where itemId=d_itemId and configId=d_configId;
IF @count<300
THEN
SELECT id,cpu,cpu1,cpu2,cpu3,diskRead,diskWrite,memory,networkReceive,networkSend,time,configId,itemId FROM MonitorInfo where itemId=itemId and configId=configId;
ELSE
SET @num= round(@count/d_count_num,0);
select @num;
set @i = 0;
select * from (
select @i :=@i + 1 as tmp_id,id,cpu,cpu1,cpu2,cpu3,diskRead,diskWrite,memory,networkReceive,networkSend,time,configId,itemId from MonitorInfo
where itemId=d_itemId and configId=d_configId) aa
where aa.tmp_id%@num=0;
END IF;
end
本文出自 “爱工作爱生活” 博客,请务必保留此出处http://4453154.blog.51cto.com/4443154/1920350
Mysql间隔取数据,实现sqlserver的row_number()函数