0

基本上,我使用此查询根据与他们关联的数字的总和对一组用户进行分组。我需要一些如何为每个结果分配索引。我对如何做到这一点感到茫然。我想我需要用 AS 给一些东西起别名,但不确定如何。有任何想法吗?

这是我动态切换页面和 per 的当前查询:

SELECT COUNT(*) as count, user_id, SUM(earnings) as sum FROM ci_league_result
 GROUP BY user_id ORDER BY sum desc LIMIT ".$page.', '.$per;

我正在寻找它可以像这样工作:

SELECT COUNT(*) as count, user_id, SUM(earnings) as sum, *NEW-RESULTS-OVERALL-INDEX* AS newindex FROM ci_league_result
GROUP BY user_id ORDER BY sum desc LIMIT ".$page.', '.$per;

注意第二个查询中的 AS newindex。

谢谢你的建议!

4

2 回答 2

1

您应该能够使用SQL 变量来完成此操作

SELECT
  COUNT(*) as count,
  user_id,
  SUM(earnings) as sum,
  @rownum := @rownum+1 AS newindex
FROM ci_league_result,
(SELECT @rownum:=0) r
GROUP BY user_id
ORDER BY sum DESC
LIMIT ".$page.', '.$per;
于 2012-12-28T22:53:26.747 回答
1

尝试这个:

SELECT user_id, count, sum, @row := @row + 1 AS newindex FROM
(SELECT
   COUNT(*) as count,
   user_id,
   SUM(earnings) as sum
FROM ci_league_result 
GROUP BY user_id ORDER BY sum desc LIMIT ".$page.', '.$per) r
CROSS JOIN (SELECT @row := 0) rr;

已编辑

于 2012-12-28T22:53:45.013 回答