Four kinds of databases, the method of taking random records

Source: Internet
Author: User
Tags count empty end 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%>

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.