使用SQL语句随机获取数据
程序员文章站
2022-05-25 15:50:39
...
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
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
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)+"字段)";