MYSQL查找从小到大排列第90%个位置的数据

 

方法一(存储过程实现):

 

begin
DECLARE cnt INT DEFAULT 0;
select count(*) into cnt from user;
set cnt = round(cnt*0.9)-1;
select * from user limit cnt, 1;
end

 

 

方法二(直接查询,,只有查询权限的时候可以考虑):

这种方法也是解决mysql limit后面不支持变量的问题

SELECT COUNT(*) INTO @cnt FROM cmb_loading_count_20141013 WHERE ver>=1003002251;
SET @cnt = ROUND(@cnt*0.9)-1;
SET @stmt = ‘select loading_time from cmb_loading_count_20141013 where ver>=1003002251 order by loading_time asc limit ?,?‘;
PREPARE s1 FROM @stmt;
SET @end = 1;
EXECUTE s1 USING @cnt,@end;
DEALLOCATE PREPARE s1;

 

 

参考:

http://www.2cto.com/database/201309/241093.html

 

MYSQL查找从小到大排列第90%个位置的数据

上一篇:阿里云512M主机(CentOS 6.5)编译安装MySQL 5.6.21遇到的问题


下一篇:Java实现MySQL数据库备份