MySQL queries

From Ilianko
Revision as of 09:36, 4 August 2012 by Anko (talk | contribs) (Created page with "===Избор на произволен ред:=== Най-елементарен, но бавен SELECT * FROM `table` ORDER BY RAND() LIMIT 0,1; Сравнително бъ...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Избор на произволен ред:

Най-елементарен, но бавен

SELECT * FROM `table` ORDER BY RAND() LIMIT 0,1;

Сравнително бърз

SELECT * FROM `table` WHERE id >= (SELECT FLOOR( MAX(id) * RAND()) FROM `table` ) ORDER BY id LIMIT 1;

Най-бърз метод

$offset_result = mysql_query( " SELECT FLOOR(RAND() * COUNT(*)) AS `offset` FROM `table` ");
$offset_row = mysql_fetch_object( $offset_result ); 
$offset = $offset_row->offset;
$result = mysql_query( " SELECT * FROM `table` LIMIT $offset, 1 " );