SQL Server CE - Select random rows

孤街醉人 提交于 2020-01-05 07:07:19

问题


How do you select a random rows from a table?

For example, if there are 1000 rows in a table matching the criteria that I want, I want to select just 20 random ones.

Like TOP, but random.

Thanks, and this is for SQL Server CE 3.5!


回答1:


How about this?

 SELECT TOP(20) * FROM myTable ORDER BY NEWID()



回答2:


How about SELECT * FROM tbl_name ORDER BY RAND() LIMIT 0,20; However if your db is with million rows Both newid() and rand() would perform slow. There is a faster solution .Read this



来源:https://stackoverflow.com/questions/9962010/sql-server-ce-select-random-rows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!