SELECT TOP 10 OrderID, NewID() as Random
FROM Orders
ORDER BY Random
关键在于NewID()这个函数,他生成一个随机数。
随机提取10条记录的例子:
SQL Server:
Select Top 10 * From 表 Order By NewID()
Access:
Select Top 10 * From 表 Order By Rnd(ID)
Random rnd = new Random(unchecked((int)DateTime.Now.Ticks));
int intRandomNumber = rnd.Next();
Rnd(ID) 其中的ID是自动编号字段,可以利用其他任何数值来完成,比如用姓名字段(UserName)
Select Top 10 * From 表 Order BY Rnd(Len(UserName))
MySql:
Select * From 表 Order By Rand() Limit 10
SQL语句真是博大精深阿~