MySQL - New value which wasn't in the column before
#4

Quote:
Originally Posted by TudvariHUN
Посмотреть сообщение
Yes,the auto increment would be very easy to use,but it wouldn't be realistic.
You could write a simple query that generates a random number that isn't in the database yet. Note that, because 5 digits will only run from 00000 to 99999 that this is not a good solution when you have a lot of users. I'd say after 5-10K users you will need to rethink this tactic and move to 6 or 7 digit numbers.

The query could be:
PHP код:
SELECT FLOOR(RAND() * 99999) AS random_generated_number
FROM my_table
WHERE random_generated_number NOT IN 
(
    
SELECT phone_number
    FROM my_table
)
LIMIT 1 
What this does is, it will return a set of randomly generated numbers (between 0 and 99999 because RAND() returns a double from 0 to 1) that are not already in my_table. LIMIT 1 means it will limit the set to the first occurence.

More here:
http://stackoverflow.com/questions/4...ber-generation
Reply


Messages In This Thread
MySQL - New value which wasn't in the column before - by TudvariHUN - 04.01.2014, 09:59
Re: MySQL - New value which wasn't in the column before - by Vince - 04.01.2014, 11:31
Re: MySQL - New value which wasn't in the column before - by TudvariHUN - 04.01.2014, 11:46
Re: MySQL - New value which wasn't in the column before - by Sinner - 04.01.2014, 13:50

Forum Jump:


Users browsing this thread: 1 Guest(s)