Php MySQL将select与其他表中的sum组合起来

Php MySQL将select与其他表中的sum组合起来,php,mysql,sql,select,sum,Php,Mysql,Sql,Select,Sum,我对MySQL不太感兴趣,但我只需要一句话,我非常感谢您在这方面的帮助 我有两个表格:“用户”和“分数” 以下是“用户”的结构: | user_id | user_name | | 1 | Paul | | 2 | Peter | 下面是“分数”的结构: | score_id | score_user_id | score_track_id | score_points | | 1 | 2 | 23

我对MySQL不太感兴趣,但我只需要一句话,我非常感谢您在这方面的帮助

我有两个表格:“用户”和“分数”

以下是“用户”的结构:

| user_id | user_name |
| 1       | Paul      |
| 2       | Peter     |
下面是“分数”的结构:

| score_id | score_user_id | score_track_id | score_points | 
| 1        | 2             | 23             | 200          |
| 2        | 2             | 25             | 150          |
现在,我需要一个查询,它为我提供某种高分列表。结果应该包含用户id、用户名以及与用户相关的所有分数的总和:我应该如下所示:

| user_id | user_name | scores |
| 1       | Paul      | 0      |
| 2       | Peter     | 350    |
| position | user_id | user_name | scores |
| 1        | 2       | Peter     | 350    |
| 2        | 1       | Paul      | 0      |
如果结果按照用户在全球排名中的排名顺序排序,则更好:

| user_id | user_name | scores |
| 1       | Paul      | 0      |
| 2       | Peter     | 350    |
| position | user_id | user_name | scores |
| 1        | 2       | Peter     | 350    |
| 2        | 1       | Paul      | 0      |
我试过了

SELECT user_id as current_user, user_name, SUM(SELECT score_points FROM score WHERE score_user_id = current_user) as ranking FROM user ORDER BY ranking DESC
这会导致语法错误。 对我来说,主要的问题是将“user”中的user\u id连接到每行“score”中的score\u user\u id


非常感谢您的帮助

您只需按用户对分数进行分组:

选择@p:=@p+1作为位置t* 从…起 选择user.user\u id, user.user\u名称, IFNULLSUMscore.得分,0作为总分 从user.user\u id=score.score\u user\u id上的用户左连接分数 按用户分组。用户\u id 按总积分说明排序 作为t连接,选择@p:=0作为初始化 在电视上看