How to request a random row in SQL?

前端 未结 29 3609
孤城傲影
孤城傲影 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:17

    You didn't say which server you're using. In older versions of SQL Server, you can use this:

    select top 1 * from mytable order by newid()
    

    In SQL Server 2005 and up, you can use TABLESAMPLE to get a random sample that's repeatable:

    SELECT FirstName, LastName
    FROM Contact 
    TABLESAMPLE (1 ROWS) ;
    

提交回复
热议问题