mysql - How to use SQL to get records that are distributed evenly from each category? -
i have table this:
category_id uid popularity ts ======================================= 03 05 07 07 07 10 10
for each row, uid
unique , each row belongs category, presented category_id
. let's have 10 categories in total , there thousands of records.
i try make sql query 50 records within timeframe sorted popularity. can done. however, if hope these 50 records distributed evenly each category? example, if there 10 categories, each has 5 records, there 50 records in total. there way via 1 sql query? or have in 10 separate sql queries?
select * mytable categoryid = 1 order popularity desc limit 10 union select * mytable categoryid = 2 order popularity desc limit 10 union select * mytable categoryid = 3 order popularity desc limit 10
repeat many categories have
some mysql wizard can tell how group by too.
see: unions
Comments
Post a Comment