How to Generate Random number without repeat in database using PHP?

SELECT FLOOR(RAND() * 99999) AS random_num
FROM numbers_mst 
WHERE "random_num" NOT IN (SELECT my_number FROM numbers_mst)
LIMIT 1

What this does:

  1. Selects random number between 0 – 1 using RAND().
  2. Amplifies that to be a number between 0 – 99999.
  3. Only chooses those that do not already exist in table.
  4. Returns only 1 result.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)