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) + "*自動編號字段)"
