How do I select a fixed number of rows for each group?

后端 未结 3 1421
[愿得一人]
[愿得一人] 2020-12-31 17:10

Here is some example data in a mysql table

a   b   distance
15  44  250
94  31  250
30  41  250
6   1   250
95  18  250
72  84  500
14  23  500
55  24  500
9         


        
3条回答
  •  感动是毒
    2020-12-31 17:49

    I wonder if this will work?

    SELECT 
       a,b,distance 
    FROM YourTable t2
       WHERE ROW(a,b,distance) IN 
       (
          SELECT a,b,distance FROM YourTable t1 
          WHERE t1.distance=t2.distance ORDER BY RAND() LIMIT 2
       )
    

    EDIT: unfortunately not. LIMIT is not supported in a subquery.

提交回复
热议问题