How to request a random row in SQL?

前端 未结 29 2996
孤城傲影
孤城傲影 2020-11-21 06:45

How can I request a random row (or as close to truly random as is possible) in pure SQL?

29条回答
  •  忘掉有多难
    2020-11-21 07:32

    For SQL Server 2005 and 2008, if we want a random sample of individual rows (from Books Online):

    SELECT * FROM Sales.SalesOrderDetail
    WHERE 0.01 >= CAST(CHECKSUM(NEWID(), SalesOrderID) & 0x7fffffff AS float)
    / CAST (0x7fffffff AS int)
    

提交回复
热议问题