开发者

ASP.NET Query to get Random Object

I'm looking for an efficient method of getting a single result from a SQL database randomly.

What I'm trying to accomplish is a "click here for new tip" where the tip is randomly selected from database. To simplify I don't care if this user has already seen this tip recently or ever before.

I realize I cou开发者_如何学Gold get all tips then select one at random but that seems terribly inefficient, is there a built in or easy 'Random' function?

Thanks in advance


Will it work for you (I assume you are using SQLServer) ?

SELECT TOP 1 * FROM Your_Table ORDER BY NEWID() 


Simply use the RAND function on your database: http://msdn.microsoft.com/en-us/library/ms177610.aspx. Very efficient!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜