SELECT TOP n * FROM T_table ORDER BY NEWID();
ORACLE:
SELECT FROM (SELECT FROM T_USER ORDER BY DBMS_RANDOM.RANDOM()) WHERE RONUM <= n
MySQL:
SELECT * FROM T_table ORDER BY RAND() LIMIT n
Access:
SELECT TOP n * FROM T_table ORDER BY rnd([一个自动编号字段])
这条语句在 Access 中的“查询”中是可以运行并得到随机结果的,但在 ASP.NET等后台程序代码中却无法得到预期的随机效果。
正确的写法如下:
以ASP.NET为例:
Random random = new Random(System.Guid.NewGuid().GetHashCode());
int r = random.Next();
string sql = "SELECT TOP n * FROM T_table ORDER BY RND(" + (-r) + "*自动编号字段)"