vendredi 12 août 2016

Mysql: How to correctly generate a unique 10-digit random number that does not exist on the current set of numbers

I have this query:

select t1.newval from (select LPAD(FLOOR(10000000000*RAND()), 10, '0') as 'newval') as t1 where t1.newval not in (select unqiue_no from table)

Note: the query works fine however the test is not extensive since my data is limited and the length is 10 digits.

I'm not sure if this query will never collide with the existing numbers. Since this query does not loop.

Is there any possibility that it will return an empty value if the newval number collides with the existing uqnique numbers?

Thanks in advance for your insights.




Aucun commentaire:

Enregistrer un commentaire