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

Hi!

I would liek to create a telephone system for my RPG mode.I used dini for saving,and I made the phonenumber system for dini.But nwo I use MySQL,and I would like to get a script which generates a 5 digit phonenumber,which wasn't in the column before.(=Every phonenumber is unique)
You don't have to write it for me,but I need help.
Thaks for your further help.
Reply
#2

You could use the auto increment feature. Though this means that all numbers will be sequential which could possibly be exploited. You could also use a Unix timestamp, although I can't guarantee that it will be unique if you only want to extract 5 digits.
Reply
#3

Yes,the auto increment would be very easy to use,but it wouldn't be realistic.
Reply
#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


Forum Jump:


Users browsing this thread: 1 Guest(s)