SoFunction
Updated on 2025-04-08

Four ways to randomly obtain 10 pieces of data in four databases

Four ways to randomly obtain 10 pieces of data in four databases

SQL Server:

Copy the codeThe code is as follows:

SELECT TOP 10 * FROM T_USER ORDER BY NEWID()

ORACLE:
Copy the codeThe code is as follows:

SELECT * FROM (SELECT * FROM T_USER ORDER BY DBMS_RANDOM.RANDOM()) WHERE RONUM <= 10

MySQL:
Copy the codeThe code is as follows:

SELECT * FROM T_USER  ORDER BY  RAND() LIMIT 10

Access:
Copy the codeThe code is as follows:

SELECT TOP 10 * FROM T_USER ORDER BY rnd([A automatic numbering field])

This statement can be run in the "query" in Access and gets random results, but it cannot get the expected random effects in the background program code.
The correct way to write it is as follows:

Take as an example:

Copy the codeThe code is as follows:

Random random = new Random(().GetHashCode());
int r = ();
string sql = "SELECT TOP 10 * FROM T_USER ORDER BY RND(" + (-r) + "*Auto-numbered field)"