faqts : Computers : Databases : MySQL : Language and Syntax : Queries : Mathematical Functions

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

5 of 6 people (83%) answered Yes
Recently 3 of 4 people (75%) answered Yes

Entry

Is RAND() recalculated for every row in a query?

Jan 27th, 2000 16:41
Benjamin Pflugmann, Nathan Wallace, Matthias Urlichs


RAND is calculated fresh, for every row.

http://www.mysql.com/Manual_chapter/manual_Reference.html#Mathematical_functions

This means that a query like this isn't really appropriate:

    SELECT an
    FROM databasename.alpha_numeric
    WHERE id=ROUND( (RAND()*35) 1);

The problem is that the where id= part will be recalculated for every
row, so there will be a different random number used for comparison with
each row.

Note however, that this is only true for the 3.23.x and later versions.
In older ones, rand() behaved like a constant after the first
evaluation.