SQL to Select a random row from a database table
2010-09-21 15:16
375 查看
There are lots of ways to select a random record or row from a database table. Here are some example SQL statements that don't require additional application logic, but each database server requires different SQL syntax.
Thanks Tim
Thanks Mark Murphy
Feel free to post other example, variations, and SQL statements for other database servers in the comments.
Select a random row with MySQL:
SELECT column FROM tableORDER BY RAND()LIMIT 1
Select a random row with PostgreSQL:
SELECT column FROM tableORDER BY RANDOM()LIMIT 1
Select a random row with Microsoft SQL Server:
SELECT TOP 1 column FROM tableORDER BY NEWID()
Select a random row with IBM DB2
SELECT column, RAND() as IDX FROM table ORDER BY IDX FETCH FIRST 1 ROWS ONLY
Thanks Tim
Select a random record with Oracle:
SELECT column FROM( SELECT column FROM tableORDER BY dbms_random.value )WHERE rownum = 1
Thanks Mark Murphy
Feel free to post other example, variations, and SQL statements for other database servers in the comments.
相关文章推荐
- SQL to select a random row from a database table
- Copy a table from one database to another in SQL Server 2005
- 94.You plan to move data from a flat file to a table in your database. You decide to use SQL*Loader
- How to make a random selection from an SQL table
- sql: Query to Display Foreign Key Relationships and Name of the Constraint for Each Table in Database
- strSQL = "Select * From " & strTableName & " Where False "
- How to select the data type in SQLserver database such as varchar, nvarchar
- UCenter info: MySQL Query Error SQL:SELECT value FROM [Table]vars WHERE noteexists
- SQL SERVER – Insert Data From One Table to Another Table – INSERT INTO SELECT – SELECT INTO TABLE
- Generate class from database table How can i generate a class from a table at a SQL Server?
- discuz搬家后报错SQL:SELECT value FROM [Table]vars WHERE name=’noteexists1′的解决办法
- Hive SQL 语义分析:select count(*) from tableName
- Move all database objects from one tablespace to another
- Using C# to connect to and query from a SQL database
- 数据仓库Unable to get list of procedures from database meta-data: Table 'mysql.proc' doesn't exist
- Import Data from csv file to Azure SQL DATABASE
- Import a large sql dump file to a MySQL database from command line
- discuz7.2问题SQL:SELECT * FROM [Table]notelist WHERE closed='0' AND app1'-5' LIMIT 1
- Import data from EXCEL to Oracle table with PL/SQL Developer
- Guide to Database Migration from Microsoft SQL Server using MySQL Workbench