Four kinds of databases, the method of taking random records

Source: Internet
Author: User
Tags count empty sql access
Data | database | random
Mysql:select * FROM TableName the ORDER by rand () limit 10sqlserver:select the top of the TableName Order by NEWID ()

ORACLE: A way to use DBMS random value


Sql> Select *from (select *from t Order by Dbms_random.value) where rownum<10;

A----------39 101 134 5 83 97 96 140 81

9 rows have been selected.

Sql> Select *from (select *from t Order by Dbms_random.value) where rownum<10;

A----------27 118 141 103 128 10 142 68 74 Some say it's an efficiency The worst way to ask for a better solution than this!

ACCESS (i):
Yourstr= "*1*3*4*6*12* ..." "sql=" select top * Form Yourdb where InStr (' * ' &id& ' * ', ' &yourstr& ') < >0
ACCESS (ii):
<%   n=10     ' Take any 10 records    Set rs = server. CreateObject ("Adodb.recordset")       sql = "SELECT * FROM table"        rs.open sql,conn,1,1      count=rs.recordcount   ' Total Records       if Count<>empty then          randomize               for i = 1 to n        ' cycle n times            Num=fix ( rnd*count) ' num is the number of randomly generated rows of records, with fix () so that it is not greater than the count value.            rs.move num     ' move to change random line             Response.Write Rs (0)    ' out of this record          rs.movefirst     ' Don't forget to move the pointer to the first one         next      end IF       rs.close   Set rs = nothing%>



Sql> Select *from (select *from t Order by Dbms_random.value) where rownum<10;

A----------39 101 134 5 83 97 96 140 81

9 rows have been selected.

Sql> Select *from (select *from t Order by Dbms_random.value) where rownum<10;

A----------27 118 141 103 128 10 142 68 74 Some say it's an efficiency The worst way to ask for a better solution than this!

ACCESS (i):
Yourstr= "*1*3*4*6*12* ..." "sql=" select top * Form Yourdb where InStr (' * ' &id& ' * ', ' &yourstr& ') < >0
ACCESS (ii):
<%   n=10     ' Take any 10 records    Set rs = server. CreateObject ("Adodb.recordset")       sql = "SELECT * FROM table"        rs.open sql,conn,1,1      count=rs.recordcount   ' Total Records       if Count<>empty then          randomize               for i = 1 to n        ' cycle n times            Num=fix ( rnd*count) ' num is the number of randomly generated rows of records, with fix () so that it is not greater than the count value.            rs.move num     ' move to change random line             Response.Write Rs (0)    ' out of this record          rs.movefirst     ' Don't forget to move the pointer to the first one         next      end IF       rs.close   Set rs = nothing%>

Related Article

Alibaba Cloud 10 Year Anniversary

With You, We are Shaping a Digital World, 2009-2019

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.