dimanche 4 juin 2017

Selecting random number without repetition and storing list of values in mySQL

I have a database Resources (Id, created_at, file_path) and it has more that 20k entries (id are sequential numbers). Now I want to create small random buckets of these ids and then provide each bucket to the user. The bucket size should be 100 and I want to create it randomly without any repetition. So each bucket will have a 100 random numbers.

I want to create a table Buckets (bucket_id, list_of_id). list_of_id will be the list of 100 ids taken randomly from Resources. I am having a problem in creating these buckets. How can I select 100 random ids from Resources and store it in a list ensuring that no id will be repeated in any of the buckets?

I was thinking of creating a new table for each bucket and then creating a table of these buckets but that isn't feasible as I will have to create almost 200 such tables(which will just have 100 random numbers). Upon doing a little research, I came to know that the best way to store a list is to do Normalization but I could not get my head around as to how to implement it in this case.

Any help will be appreciated.




Aucun commentaire:

Enregistrer un commentaire