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

后端 未结 11 2002
清酒与你
清酒与你 2020-12-02 11:15

I would like to generate a 5 digit number which do not repeat inside the database. Say I have a table named numbers_mst with field named my_number

相关标签:
11条回答
  • 2020-12-02 11:52

    NOTE: The other solutions posted will work only if the column is configured as NOT NULL. If NULL, it'll just return no results. You can fix the query like this:

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

    ... The ...WHERE my_number IS NOT NULL is necessary!

    EDIT: I just wanted to mention that I intentionally removed the inner SELECT's table name because it didn't seme necessary and seemed to break if there was no data in the table yet? However, maybe this was intentionally included? — Please clarify or comment for everyone, thanks.

    0 讨论(0)
  • 2020-12-02 11:56

    This is easiest method to build unique code generator without check database, it will save database query execution time.

    function unique_code_generator($prefix='',$post_fix='')
        {
            $t=time();
            return ( rand(000,111).$prefix.$t.$post_fix);
        }
    

    Enjoy, have a nice coding day..

    :)

    0 讨论(0)
  • 2020-12-02 11:58

    I know my answer is late, but if anyone is looking for this subject in the future, who would not have a random number with leading zero, you should add the LPAD() function.

    So your query it will like

    SELECT LPAD(FLOOR(RAND()*99999),5,0)
    

    Have a nice day.

    0 讨论(0)
  • 2020-12-02 11:58

    We can simply do with this:

    $regenerateNumber = true;
    
    do {
        $regNum      = rand(2200000, 2299999);
        $checkRegNum = "SELECT * FROM teachers WHERE teacherRegNum = '$regNum'";
        $result      = mysqli_query($connection, $checkRegNum);
    
        if (mysqli_num_rows($result) == 0) {
            $regenerateNumber = false;
        }
    } while ($regenerateNumber);
    

    $regNum will have the value which is not present in the database

    0 讨论(0)
  • 2020-12-02 11:58

    Very simple, I did the code in MySQL in that stored procedure

    It generates the random number of 8 digits and also uniquely with the table in database.

    It works for me.

    CREATE DEFINER=`pro`@`%` PROCEDURE `get_rand`()
    BEGIN
    DECLARE regenerateNumber BOOLEAN default true;
    declare regNum int;
    declare cn varchar(255);
    repeat
    SET regNum      := FLOOR(RAND()*90000000+10000000);
    SET cn =(SELECT count(*) FROM stock WHERE id = regNum);
    select regNum;
    if cn=0
    then
    SET regenerateNumber = false;
    end if;
    UNTIL regenerateNumber=false
    end repeat;
    END
    
    0 讨论(0)
  • 2020-12-02 12:04

    You have two approaches:

    The first, suggested in other answer, is to create a random number (using mt_rand() ) and check it's not in the database. If it is in the database, regnerate and try again. This is simplest if you are generating a single number - see other answers for code. But if you are gnerating more that 50% of the numbers it will be very slow and inefficient.

    If ou want a lot of numbers, the alternative is to populate a database with all the records and have a column "picked". Run a query to find how many are "not picked" and then find a random number between 0 and the number "not picked". Then run the SQL query to get the number in that position (where not picked, use LIMIT in mysql) and mark as picked. A bit convoluted, it's more work and less efficient if you only want a few numbers, but will be much better when you want to get more that 50% (estimate) of the numbers.

    Note: you can make it more efficient by storing the count selected locally and running a few less queries.

    0 讨论(0)
提交回复
热议问题