unique random integer sql

Started on the old forum in September 2020 · view the original in the archive · old links redirect here

1 reply · 94 views · started 6 years ago · 1 min read

2 participants
something else Fellow · 2,296 posts #1

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)

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:

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
rishab Newbie · 3 posts #2

Thanks for sharing this.

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close