SQL fetch data randomly selected number

 method one:

Demand is to remove the n pieces of random data at Home to recommend to the user, a random id began to think, based on the maximum and minimum id id to random, think about or forget.

 

Later found also order by rand (), a smooth solution to this demand

select * from table where 1 order by rand() limit 10

 Method Two:

Want randomly selected number of samples in SQL,

The main idea: one is randomly generated random number, the random number and sort, then the line 1000 prior to extraction
following example:
# 1000 samples randomly selected from the total sample, stored in the table table_name

 

create table table_name as # Create a table table_name 
the SELECT b. * from (
the SELECT RAND () AS index_name, A. * from table_name_a AS A # table table_name_a assign a random number) as b # new table assignment for the table b
the Order by b.index_name desc # b on the random number table to sort index_name
limit 1000 # 1000 to take before sorting

  

LIMIT brief:
 the SELECT * from table_name

 limit 100,10 # 101 extracted from the first row of data samples, a total of 10 samples were drawn

 method one:

Demand is to remove the n pieces of random data at Home to recommend to the user, a random id began to think, based on the maximum and minimum id id to random, think about or forget.

 

Later found also order by rand (), a smooth solution to this demand

select * from table where 1 order by rand() limit 10

 Method Two:

Want randomly selected number of samples in SQL,

The main idea: one is randomly generated random number, the random number and sort, then the line 1000 prior to extraction
following example:
# 1000 samples randomly selected from the total sample, stored in the table table_name

 

create table table_name as # Create a table table_name 
the SELECT b. * from (
the SELECT RAND () AS index_name, A. * from table_name_a AS A # table table_name_a assign a random number) as b # new table assignment for the table b
the Order by b.index_name desc # b on the random number table to sort index_name
limit 1000 # 1000 to take before sorting

  

LIMIT brief:
 the SELECT * from table_name

 limit 100,10 # 101 extracted from the first row of data samples, a total of 10 samples were drawn

Guess you like

Origin www.cnblogs.com/huangchenggener/p/10985713.html