Home  >  Article  >  Database  >  mysql随机取数_MySQL

mysql随机取数_MySQL

WBOY
WBOYOriginal
2016-06-01 13:06:591097browse

order by RAND() 效率太差.

我的主键为string,网上的高效语句也无法用上.于是自己琢磨了个新的随机取数处理方法. 未做测试.先记录下.

第一步,获取将要获取的数量的总数量.

 string hql_nums = "select COUNT(*) from Table where a='" + a+ "'"; int allnums = MysqlHibernate.GetInstance().getCout(hql_nums);


第二步.

  Random r = new Random();  int begin = r.Next(allnums - nums); //nums为需要取值数量.防止取数不够

第三步. 以begin为起点, 取2*nums 条数据.然后对数据进行排序.获取需要的nums条数据.




Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn