0

Possible Duplicate:
Retrieving the last record in each group

With the below query I'd get the results one and three. How do I get the results for sid; 15 and 17? I can't use a WHERE because I won't know the sid.

A better way of explaining is, how do I LIMIT per sid without grouping?

mysql_query("SELECT *
             FROM   `mytable`
             GROUP  BY `sid`
             ORDER  BY `sid` ASC
             LIMIT  0, 2");

+----+-----------+----------+
| id |       sid |      num |
+----+-----------+----------+
|  1 |        15 |      one |   
|  2 |        15 |      two |   
|  3 |        17 |    three |   
|  4 |        17 |     four |  
|  5 |        18 |     five |   
|  6 |        18 |      six | 
Community
  • 1
  • 1
user892134
  • 3,078
  • 16
  • 62
  • 128
  • I don't understand you: do you want all/smaller/bigger records whose `sid` field is `15` or `17`? – Nicolás Ozimica Nov 28 '11 at 20:30
  • basically if i do limit 0,2 it should get only two sid's(15 and 17, four results). If i do limit 0,3 it get's three sid's(15,17 and 18, six results). – user892134 Nov 28 '11 at 20:33
  • Try with: mysql_query("SELECT * FROM `mytable` WHERE `sid` IN ( SELECT DISTINCT `sid` FROM `mytable` ORDER BY `sid` ASC LIMIT 0, 2 )"); – Nicolás Ozimica Nov 28 '11 at 20:49

0 Answers0