sql - MySQL: Last 10 entries per user? -


i have table storing transactional data users. find users rating take average score of last 10 entries user. there way sql?

i need able work out single user given id. , list of users ordered score.

currently i'm working out outside mysql , storing in col each user can order_by that.

# returns average transactions. select user_id, avg(score) transactions user_id = 1 # returns scores last 10 transactions. select user_id, score transactions user_id = 1 order_by date desc limit 10 

use:

  select x.user_id,          avg(x.score)     (select t.user_id,                  t.score             transactions t            t.user_id = ?         order t.date            limit 10) x group x.user_id 

Comments

Popular posts from this blog

ruby - When to use an ORM (Sequel, Datamapper, AR, etc.) vs. pure SQL for querying -

php - PHPDoc: @return void necessary? -

c++ - Convert big endian to little endian when reading from a binary file -