php - What is the best way to select the first two records of each group by a "SELECT" command? -


for instance have following table:

id group data 1 1 aaa 2 1 aaa 3 2 aaa 4 2 aaa 5 2 aaa 6 3 aaa 7 3 aaa 8 3 aaa 

what best way select first 2 records of each group "select" command? if there no way so, routine suggest?(in php)

(model outcome)

1 1 aaa 2 1 aaa 3 2 aaa 4 2 aaa 6 3 aaa 7 3 aaa 

i knew cross-joining a.id >= b.id in sub-query can working looking more scalable solution can applied on table millions of records. thanks

select a.* tablename  (    select count(*)     tablename b    a.group = b.group , a.id >= b.id ) <= 2 

Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -