Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql 1 Random Row [duplicate]

Tags:

php

mysql

Possible Duplicate:
How to request a random row in SQL?

Is this the correct way to do this?

$query = 'SELECT * FROM gameids ORDER BY timestamp RAND LIMIT 1';
like image 618
AndrewFerrara Avatar asked Feb 08 '11 21:02

AndrewFerrara


2 Answers

Incorrect. You cant order by a column (afaik) if you want it to randomize.

$query = 'SELECT * FROM gameids ORDER BY RAND() LIMIT 1';
like image 162
Marwelln Avatar answered Sep 20 '22 13:09

Marwelln


You don't need to tell it which column to randomise, but you do need () after RAND because it is a function.

SELECT
  * 
FROM
  gameids 
ORDER BY 
  RAND()
LIMIT 1
like image 41
Alan Whitelaw Avatar answered Sep 17 '22 13:09

Alan Whitelaw