unique random integer sql

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    unique random integer sql

    I often find the need to find a unique random integer from a database ... which is handy for identifying rows without them going in numerical order
    (So people can't see how many rows are in the database)

    Code:
    SELECT FLOOR(10000 + RAND() * 89999) AS random_number FROM table WHERE random_number NOT IN (SELECT unique_id FROM table) LIMIT 1
    note: the above code requires an integer to be already in the database to work.

    or here is a version that works with nulls:
    Code:
    SELECT FLOOR(10000 + RAND() * 89999) AS my_tracker FROM Table1 WHERE "tracker" NOT IN (SELECT tracker FROM Table1 WHERE tracker IS NOT NULL) LIMIT 1
Working...
X