Search
 
SCRIPT & CODE EXAMPLE
 

SQL

mysql order by rand limit 1 really slow

function get_random_row($mytable,$conn){
	$table_max_id = $conn->query("SELECT id FROM $mytable ORDER BY id DESC LIMIT 0, 1")[0]["id"];
	$rand_id=rand(1,$table_max_id);
	$random_result = $conn->query("select * from $mytable where id = $rand_id");
  	
	#note: if you are missing some id's, we can call again until we get hit
  	if(!$random_result){
    	return get_random_row($mytable,$conn);
    }
  
  	return $random_result;
}
Comment

PREVIOUS NEXT
Code Example
Sql :: How to take sum of column with same id and different table in SQL? 
Sql :: sql field equals multiple values 
Sql :: mssql now diff 90 day 
Sql :: mysql update from n to 100 
Sql :: delete account in flask and sqlalchemy 
Sql :: full-text index mysql 
Sql :: cql insert 
Sql :: left join sql 
Sql :: postgres meta command to show all rows in table 
Sql :: how to use select union and loop 
Sql :: rownum in sql 
Sql :: psql initialization 
Sql :: postrgesql concat 2 columns divided by ; 
Sql :: Truncate a table then insert data 
Sql :: what is subquery in sql 
Sql :: denormalization in sql example 
Sql :: mysql not 
Sql :: postgres advance table id count 
Sql :: mysql sleep connections 
Sql :: sqlite3 python foreign key 
Sql :: oracle sql count occurrences of value in column 
Sql :: Oracle SQL join three tables and group by column 
Sql :: sql change primary key to composite key 
Sql :: mysql write into table 
Sql :: sql and operator 
Sql :: sql create table as 
Sql :: SQL Add Multiple Columns in a Table 
Sql :: oracle multiple insert 
Sql :: mysql procedure 
Sql :: postgresql gset 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =