dimanche 3 novembre 2019

Limit and order by rand - in groups

Maybe I am confused, but I seem to be on the wrong way to find a solution right now.

I would like to select random data from a table, but with some limits Let's say the table has a NAME and AGE...10.000 rows

I would like to select (in that order)

  • 2 random people that are older than 60 years
  • 2 random people that are older than 40 years (includes over 60 too)
  • 2 random people at any age.

And of course all distinct.

Result

Andy 77
Maria 64
Harry 54
Fritz 62
Anna 24
Moses 43

What's your best solution?

I though UNION and LIMIT would work with ORDER BY RAND()...but no..not allowed Order by with limits?




Aucun commentaire:

Enregistrer un commentaire