随机排序查询访问
Random sorting query Access
我正在使用这个简单的查询对 ms-access 数据库进行随机排序:
SELECT pk FROM TABLE ORDER BY Rnd(pk) asc
当我用 Microsoft Access 2010 测试它时它工作正常
但是,当我使用经典 asp 调用此查询时,随机排序不起作用。
这是我的代码:
set Rs = Server.CreateObject("ADODB.Recordset")
Rs.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("/dbfolder") & "\dbname.mdb"
Rs.Source = "SELECT pk FROM TABLE ORDER BY Rnd(pk) asc"
Rs.CursorType = 3
Rs.CursorLocation = 2
Rs.LockType = 3
Rs.Open()
do while not Rs.eof
'do stuff
Rs.Movenext
loop
终于找到解决办法了
来源:http://www.cosnetics.co.uk/articles/select-random-records-from-access/
按照上面写的link
Here is a very simple way of selecting random records from an Access
database:
SELECT TOP 5 * FROM [tableName] ORDER BY rnd(INT(NOW*id)-NOW*id)
The only prerequisite is that you have an AutoNumber Id column. It
should also be noted that although this generates a random set of
records, they are not truely random in a mathematical sense, but
should be sufficiently random for most uses.
This method of selecting records also works in sql server, but you
have to change rnd to rand.
如果您找到更好的解决方案,我很乐意将其标记为最佳解决方案。与此同时,我会把它留在这里。希望这会帮助别人。 (你不会相信,但我花了一些时间才找到它)
您只需要“salt”您的随机生成器:
Rs.Source = "SELECT pk FROM TABLE ORDER BY Rnd(-Timer() * [pk]) Asc"
我正在使用这个简单的查询对 ms-access 数据库进行随机排序:
SELECT pk FROM TABLE ORDER BY Rnd(pk) asc
当我用 Microsoft Access 2010 测试它时它工作正常
但是,当我使用经典 asp 调用此查询时,随机排序不起作用。
这是我的代码:
set Rs = Server.CreateObject("ADODB.Recordset")
Rs.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("/dbfolder") & "\dbname.mdb"
Rs.Source = "SELECT pk FROM TABLE ORDER BY Rnd(pk) asc"
Rs.CursorType = 3
Rs.CursorLocation = 2
Rs.LockType = 3
Rs.Open()
do while not Rs.eof
'do stuff
Rs.Movenext
loop
终于找到解决办法了
来源:http://www.cosnetics.co.uk/articles/select-random-records-from-access/
按照上面写的link
Here is a very simple way of selecting random records from an Access database:
SELECT TOP 5 * FROM [tableName] ORDER BY rnd(INT(NOW*id)-NOW*id)
The only prerequisite is that you have an AutoNumber Id column. It should also be noted that although this generates a random set of records, they are not truely random in a mathematical sense, but should be sufficiently random for most uses.
This method of selecting records also works in sql server, but you have to change rnd to rand.
如果您找到更好的解决方案,我很乐意将其标记为最佳解决方案。与此同时,我会把它留在这里。希望这会帮助别人。 (你不会相信,但我花了一些时间才找到它)
您只需要“salt”您的随机生成器:
Rs.Source = "SELECT pk FROM TABLE ORDER BY Rnd(-Timer() * [pk]) Asc"