Mysql
 sql >> Database >  >> RDS >> Mysql

Come ottenere record RANDOM da ciascuna categoria in MySQL?

Sì, puoi farlo enumerando le righe e poi recuperando le prime tre:

select r.id, r.question, r.category
from (select r.*,
             (@rn := if(@c = category, @rn + 1,
                        if(@c := category, 1, 1)
                       )
             ) as seqnum
      from `random` r cross join
           (select @rn := 0, @c := -1) params
      order by category, rand()
     ) r
where seqnum <= 3;