mysql - Rails query selecting last record with distinct column -
i need make query retrieve "last" records of distinct column.
i have model, languages, has 3 columns:
locale -> es | en | de version -> 1 | 2 | 3 data -> aaa | bbb | ccc
there can duplicated records locale, not version. example:
es | 1 | aaa es | 2 | aab es | 3 | aba en | 1 | bbb en | 2 | bab
and in example, need retrieve result:
es | 3 | aba en | 2 | bab
there last 2 versions of language. i'm doing query right now:
language.all.order('created_at desc').group(:locale)
but i'm getting:
es | 2 | aba en | 1 | bab
any ideas? in advance!
here raw mysql query give result set want:
select lang1.* languages lang1 inner join ( select l.locale, max(l.version) maxversion languages l group l.locale ) lang2 on lang1.locale = lang2.locale , lang1.version = lang2.maxversion
i not ruby person, as can offer. i'm hoping know how translate mysql query query language have in op.
Comments
Post a Comment