How to generate random numbers starting from 00000 to 99999 without repetition using Mysql only

Asked

Viewed 541 times

1

How to generate random numbers starting from 00000 to 99999 without repetition using Mysql only, numbers cannot start in 000, anyone has any idea how I can do this?

  • 1

    Search about the function rand(). With it you can already have an idea of how to do.

  • What is the limit? How many random numbers?

  • What do you mean, you can’t start at 1000? Numbers never start at 0. What you want is 100 forward so?

  • I think he meant bigger than 100 (00100)

  • @Leocaracciolo is, I thought one thing and wrote another, I think it’s this, but I want him to confirm, it’s weird.

  • Maybe this will help https://stackoverflow.com/questions/11680025/how-to-generate-random-number-without-repeat-in-database-using-php/40549385#40549385 please amend WHERE my_number IS NOT NULL) for WHERE my_number >100)

  • For use with INSERT or SELECT?

Show 2 more comments

1 answer

1

Good morning Bruno. I think you can use this here:

select FLOOR(RAND() * 99999)

Or rather:

BEGIN


DECLARE valorRand bigint;
        set valorRand = select FLOOR(RAND() * 99999);
        if exists (select * from tabela where coluna = valorRand) then
            call meuRandon();
        end if

        return valorRand;

then you have to see how many times it can be repeated.

  • Forehead in your mysql

  • 1

    And how do you make sure this doesn’t get repeated?

  • 1

    @Maniero does not guarantee. rsrsrs

  • 2

    @cpll thanks for the force, but there can be no repetitions.

  • @Bruno Richart, you need to shape the idea better about its rules and needs, but I tried to make the function, only return a value when it does not yet exist. I did recursive, but the idea can be improved and done in functions.

  • 1

    @cpll yes true, but helped a lot I’ve managed, thank you very much.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.