Home > Database > Mysql Tutorial > body text

mysql随机取数_MySQL

WBOY
Release: 2016-06-01 13:06:59
Original
1133 people have browsed it

order by RAND() 效率太差.

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

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

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


第二步.

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

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




source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template