日期:2014-05-18  浏览次数:20365 次

一道面试题!
在数据库的商品表中有n (n>1) 条数据,现在我们要随机取一条商品数据,请写出SQL语句(不要采用order by 随机列的方式,请用类分页算法解答)。商品表表名为Product_Table , 取随机数的函数为 Random ( beginNum , endNum ),其中beginNum和endNum为随机数产生的闭合区间的起点值和结束值


------解决方案--------------------
SQL code

--没有random函数
--选择n到m条数据
1--方法
select top  m-n+1 *  from tablea where ID not in (select top n-1 ID from tablea)

2--方法
select top m-n * from 
(select top m * from Tablec order by ID asc)a