0

guys im using the laravel framework version 3 , i wanna to get the most 10 active users (who uploaded huge number of pictures) . my tables structres : Users :

id,username ..

Images :

id,user_id,views..

then i want to show the total of views of their pictures exmaple : views of the pic 1 is : 150 views of the pic 2 is : 100 so the total of that users will be 250 and so on . can anyone help plz?

4

1 回答 1

0

SQL 查询将是这样的:

SELECT
  COUNT(*) as numPics,
  SUM(p.views) as totalViews
FROM
  pics p
INNER JOIN
  users u
ON
  p.user_id = u.id
GROUP BY p.user_id
ORDER BY numPics DESC LIMIT 10

但是,根据您拥有的数据量,我强烈建议您避免这种实时聚合并寻求缓存它。

于 2013-04-12T16:49:40.540 回答