jeudi 12 novembre 2020

How can I randomly select n rows per group a different number of times for each group in MySQL?

I basically have two tables, both of which contain two columns - a list of people ID and their birth year, like this:

ID birth_year

1 1981

2 1982

3 1982

4 1983

etc

For each person in table 1 I need to select 6 people at random with the same birth year from table 2. I think I can use ORDER BY RAND() and LIMIT 6 in this query. My issue is that in table 1 there are many with the same birth year, maybe 102 people born in 1987 and 88 people born in 1988. How can I write the query such that it selects 6 random people born in 1987 102 times, and 88 times for 1988?




Aucun commentaire:

Enregistrer un commentaire