SA-MP Forums Archive
MySQL - New value which wasn't in the column before - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: MySQL - New value which wasn't in the column before (/showthread.php?tid=485440)



MySQL - New value which wasn't in the column before - TudvariHUN - 04.01.2014

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.


Re: MySQL - New value which wasn't in the column before - Vince - 04.01.2014

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.


Re: MySQL - New value which wasn't in the column before - TudvariHUN - 04.01.2014

Yes,the auto increment would be very easy to use,but it wouldn't be realistic.


Re: MySQL - New value which wasn't in the column before - Sinner - 04.01.2014

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