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