萬盛學電腦網

 萬盛學電腦網 >> 數據庫 >> mssql數據庫 >> 使用SQL語句隨機獲取數據

使用SQL語句隨機獲取數據

   SQL Server--> SELECT TOP 50 * FROM TAB_USERS ORDER BY NEWID();

  ORACLE--> SELECT * FROM (SELECT * FROM TAB_USERS ORDER BY DBMS_RANDOM.RANDOM()) WHERE RONUM<=50

  MYSQL--> SELECT * FROM TAB_USERS ORDER BY RAND() LIMIT 50

  {

  ACCESS--> SELECT TOP 50 * FROM TAB_USERS ORDER BY RND([字段])

  這條語句在ACCESS中的查詢結果是正確的,但在ASP.NET等後台代碼要寫成如下:

  Random random=new Random(System.Guid.NewGuid().GetHashCode());

  int val=random.Next();

  string sql="SELECT TOP 50 * FROM TAB_USERS ORDER BY RND("+(-r)+"字段)";

  }

copyright © 萬盛學電腦網 all rights reserved