如何从数据库中选择随机记录 [英] how to choose a random record from a database

查看:102
本文介绍了如何从数据库中选择随机记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

生病了一个包含1个表和3个字段的数据库:


ID FIRSTNAME LASTNAME


(ID字段将自动递增索引)


数据库中可能有10条记录,可能有10,000条。

i需要打开数据库并随机选择一条记录(然后显示)

名称,我没有问题)

如何随机选择一条记录?即时猜测ID必须打开一个记录集

并检查计数以获得记录数量,所以我们说有100个

记录。我想我必须在1和

100之间生成一个随机数....


任何人都有一个小例子?

ill have a database with 1 table and 3 fields:

ID FIRSTNAME LASTNAME

(the ID field will be the auto incrementing index)

there might be 10 records in the DB, there might be 10,000.
i need to open the DB and randomly select a record (and then display the
name, which i dont have a problem with)
how can i randomly select a record? im guessing id have to open a recordset
and check the count to get the number of records, so lets say there were 100
records. i imagine i would have to generate a random number between 1 and
100....

anyone have a small example?

推荐答案

http://databases.aspfaq.com/database...om-record.html

" Jimmy" < j@j.jwrote in message

news:或************** @ TK2MSFTNGP04.phx.gbl ...
http://databases.aspfaq.com/database...om-record.html

"Jimmy" <j@j.jwrote in message
news:Or**************@TK2MSFTNGP04.phx.gbl...

ill有一个包含1个表和3个字段的数据库:


ID FIRSTNAME LASTNAME


(ID字段将是自动递增索引)


数据库中可能有10条记录,可能有10,000条。

i需要打开数据库并随机选择一个记录(然后显示

名称,我没有问题)

如何随机选择一条记录?我猜测id必须打开一个

记录集并检查计数以获得记录数,所以我们说

有100条记录。我想我必须在1到100之间生成一个随机数

....


任何人都有一个小例子?

ill have a database with 1 table and 3 fields:

ID FIRSTNAME LASTNAME

(the ID field will be the auto incrementing index)

there might be 10 records in the DB, there might be 10,000.
i need to open the DB and randomly select a record (and then display the
name, which i dont have a problem with)
how can i randomly select a record? im guessing id have to open a
recordset and check the count to get the number of records, so lets say
there were 100 records. i imagine i would have to generate a random number
between 1 and 100....

anyone have a small example?



谢谢...但问题很少。这是代码:


<%

随机化()

randNum =(CInt(1000 * Rnd)+ 1 )* -1


set conn = CreateObject(" ADODB.Connection")

sql =" SELECT TOP 1 cols," ; &安培; _

" r = Rnd("& randNum&")" &安培; _

" FROM TableName" &安培; _

" ORDER BY r"


set rs = conn.execute(sql)

response.write rs(0)


''...

rs.close:set rs = nothing

conn.close:set conn = nothing

%>


什么randNum相等?什么和什么之间的数字?

在SQL语句中,什么是cols?和r?


" Aaron Bertrand [SQL Server MVP]" < te ***** @ dnartreb.noraawrote留言新闻:OD ************** @ TK2MSFTNGP02.phx.gbl ...
thanks... few questions though. here is the code:

<%
Randomize()
randNum = (CInt(1000 * Rnd) + 1) * -1

set conn = CreateObject("ADODB.Connection")

sql = "SELECT TOP 1 cols," & _
"r = Rnd(" & randNum & ")" & _
"FROM TableName " & _
"ORDER BY r"

set rs = conn.execute(sql)

response.write rs(0)

'' ...
rs.close: set rs = nothing
conn.close: set conn = nothing
%>

what will randNum equal? a number between what and what?
in the SQL statement, what is "cols"? and "r"?

"Aaron Bertrand [SQL Server MVP]" <te*****@dnartreb.noraawrote in message news:OD**************@TK2MSFTNGP02.phx.gbl...
http:// databases.aspfaq.com/database...om-record.html


" Jimmy" < j@j.jwrote in message

news:或************** @ TK2MSFTNGP04.phx.gbl ...
http://databases.aspfaq.com/database...om-record.html

"Jimmy" <j@j.jwrote in message
news:Or**************@TK2MSFTNGP04.phx.gbl...

>生成一个包含1个表和3个字段的数据库:

ID FIRSTNAME LASTNAME

(ID字段将自动递增索引)

数据库中可能有10条记录,可能有10,000条记录。
我需要打开数据库并随机选择一条记录(然后显示
名称,哪个我没有问题)
我如何随机选择一个记录?即时猜测ID必须打开一个
记录集并检查计数以获得记录数,所以我们说有/或有100条记录。我想我必须在1到100之间生成一个随机数....

任何人都有一个小例子?

>ill have a database with 1 table and 3 fields:

ID FIRSTNAME LASTNAME

(the ID field will be the auto incrementing index)

there might be 10 records in the DB, there might be 10,000.
i need to open the DB and randomly select a record (and then display the
name, which i dont have a problem with)
how can i randomly select a record? im guessing id have to open a
recordset and check the count to get the number of records, so lets say
there were 100 records. i imagine i would have to generate a random number
between 1 and 100....

anyone have a small example?


randNum会等于什么?什么和什么之间的数字?


点击刷新几次:


<%

随机化( )

randNum =(CInt(1000 * Rnd)+ 1)* -1

response.write(randNum)

%>
what will randNum equal? a number between what and what?

Hit refresh a few times:

<%
Randomize()
randNum = (CInt(1000 * Rnd) + 1) * -1
response.write (randNum)
%>

在SQL语句中,什么是cols?和r?
In the SQL statement, what is "cols"? and "r"?



cols是你的列表。


r只是随机数的别名,它应该在结果中设置

,因为它用于在查询中播种随机数,并在

顺序中使用。您可以按顺序完成所有操作,但有些查询不会。



Aaron Bertrand [SQL Server MVP]" < te ***** @ dnartreb.noraawrote留言

新闻:OD ************** @ TK2MSFTNGP02.phx.gbl ...

cols is your column list.

r is just an alias for the random number, and it should be in the result set
because it use to seed the random number in the query and is used in the
order by. You could do it all in the order by but some queries will not
allow you to order by something that is not in the select list.


"Aaron Bertrand [SQL Server MVP]" <te*****@dnartreb.noraawrote in message
news:OD**************@TK2MSFTNGP02.phx.gbl...

http://databases.aspfaq.com/database...om-record.html


" Jimmy" < j@j.jwrote in message

news:或************** @ TK2MSFTNGP04.phx.gbl ...
http://databases.aspfaq.com/database...om-record.html

"Jimmy" <j@j.jwrote in message
news:Or**************@TK2MSFTNGP04.phx.gbl...

>生成一个包含1个表和3个字段的数据库:

ID FIRSTNAME LASTNAME

(ID字段将自动递增索引)

数据库中可能有10条记录,可能有10,000条记录。
我需要打开数据库并随机选择一条记录(然后显示
名称,哪个我没有问题)
我如何随机选择一个记录?即时猜测ID必须打开一个
记录集并检查计数以获得记录数,所以我们说有/或有100条记录。我想我必须在1到100之间生成一个随机的
号....

任何人都有一个小例子吗?

>ill have a database with 1 table and 3 fields:

ID FIRSTNAME LASTNAME

(the ID field will be the auto incrementing index)

there might be 10 records in the DB, there might be 10,000.
i need to open the DB and randomly select a record (and then display the
name, which i dont have a problem with)
how can i randomly select a record? im guessing id have to open a
recordset and check the count to get the number of records, so lets say
there were 100 records. i imagine i would have to generate a random
number
between 1 and 100....

anyone have a small example?




这篇关于如何从数据库中选择随机记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆