简体   繁体   中英

How do we select a random row from a mysql result?

$qry="SELECT * FROM `users` WHERE code LIKE '$querywc'";    
$res=mysql_query($qry);

this code fetches the rows based on the wildcards specified in the variable $querywc i'm sure that it'll return many rows. now what i want is, a random row from those results.

Order randomly and use limit to take just one row

SELECT * FROM `users` 
WHERE code = '$querywc'
ORDER BY rand()
LIMIT 1

执行速度更快:

SELECT TOP 1 * FROM `users` WHERE code LIKE '$querywc' order by rand();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM