Mysql间隔取数据,实现sqlserver的row_number()函数

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 :[email protected] + 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()函数的主要内容,如果未能解决你的问题,请参考以下文章

mssql怎么查询上周的数据?

mysql用视图和事件实现数据间隔插入数据到表

sqlserver 存储过程要传时间的值 我要取1年的数据 怎么实现啊?

关于sqlserver数据转储到mysql中

数据库导出数据为txt文档,间隔符都是啥意思,mysql导出到db2,oracle要做哪些设置

origin风玫瑰图怎么设置风速间隔