<bdo id='XPm1A'></bdo><ul id='XPm1A'></ul>
    <legend id='XPm1A'><style id='XPm1A'><dir id='XPm1A'><q id='XPm1A'></q></dir></style></legend>

      <i id='XPm1A'><tr id='XPm1A'><dt id='XPm1A'><q id='XPm1A'><span id='XPm1A'><b id='XPm1A'><form id='XPm1A'><ins id='XPm1A'></ins><ul id='XPm1A'></ul><sub id='XPm1A'></sub></form><legend id='XPm1A'></legend><bdo id='XPm1A'><pre id='XPm1A'><center id='XPm1A'></center></pre></bdo></b><th id='XPm1A'></th></span></q></dt></tr></i><div id='XPm1A'><tfoot id='XPm1A'></tfoot><dl id='XPm1A'><fieldset id='XPm1A'></fieldset></dl></div>

    1. <tfoot id='XPm1A'></tfoot>
    2. <small id='XPm1A'></small><noframes id='XPm1A'>

    3. MySQL将select与其他表中的sum结合起来

      MySQL combine select with sum from other table(MySQL将select与其他表中的sum结合起来)
    4. <i id='CKGAt'><tr id='CKGAt'><dt id='CKGAt'><q id='CKGAt'><span id='CKGAt'><b id='CKGAt'><form id='CKGAt'><ins id='CKGAt'></ins><ul id='CKGAt'></ul><sub id='CKGAt'></sub></form><legend id='CKGAt'></legend><bdo id='CKGAt'><pre id='CKGAt'><center id='CKGAt'></center></pre></bdo></b><th id='CKGAt'></th></span></q></dt></tr></i><div id='CKGAt'><tfoot id='CKGAt'></tfoot><dl id='CKGAt'><fieldset id='CKGAt'></fieldset></dl></div>
        <tfoot id='CKGAt'></tfoot>

      1. <small id='CKGAt'></small><noframes id='CKGAt'>

            <tbody id='CKGAt'></tbody>
          <legend id='CKGAt'><style id='CKGAt'><dir id='CKGAt'><q id='CKGAt'></q></dir></style></legend>
              <bdo id='CKGAt'></bdo><ul id='CKGAt'></ul>
              • 本文介绍了MySQL将select与其他表中的sum结合起来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我不是很喜欢 MySQL,但我只需要一个声明,我非常感谢您在这方面的帮助.

                i'm not really much into MySQL, but i need just one statement and I would really appreciate your help on this.

                我有两个表:'user' 和 'score'

                I have two tables: 'user' and 'score'

                这是用户"的结构:

                | user_id | user_name |
                | 1       | Paul      |
                | 2       | Peter     |
                

                这是'score'的结构:

                here's the structure of 'score':

                | score_id | score_user_id | score_track_id | score_points | 
                | 1        | 2             | 23             | 200          |
                | 2        | 2             | 25             | 150          |
                

                现在我需要一个能够为我提供某种高分列表的查询.结果应包含 user_id、user_name 和与用户相关的所有分数的总和:我应该如下所示:

                now I need a query that provides me some kind of highscore-list. the result should contain user_id, user_name and the sum of all scores that are related to the user: i should look like this:

                | user_id | user_name | scores |
                | 1       | Paul      | 0      |
                | 2       | Peter     | 350    |
                

                如果将结果按照用户在全球排名中的位置排序,则更好:

                even better would be, if the result would be sorted in order of the users position in the global ranking like this:

                | 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_id连接到每行'score'中的score_user_id.

                which results in a syntax error. the main problem for me is to connect the user_id from 'user' to the score_user_id in 'score' for each row.

                非常感谢您的帮助

                推荐答案

                你只需要将你的分数按用户分组:

                You just need to group your scores by user:

                SELECT @p:=@p+1 AS position, t.*
                FROM (
                  SELECT   user.user_id,
                           user.user_name,
                           IFNULL(SUM(score.score_points),0) AS total_points
                  FROM     user LEFT JOIN score ON user.user_id = score.score_user_id
                  GROUP BY user.user_id
                  ORDER BY total_points DESC
                ) AS t JOIN (SELECT @p:=0) AS initialisation
                

                在 sqlfiddle 上查看它.

                这篇关于MySQL将select与其他表中的sum结合起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                PHP Upload File Validation(PHP 上传文件验证)
                PHP Error - Uploading a file(PHP 错误 - 上传文件)
                How can I write tests for file upload in PHP?(如何在 PHP 中编写文件上传测试?)
                php resizing image on upload rotates the image when i don#39;t want it to(php在上传时调整图像大小会在我不想要它时旋转图像)
                How to send additional data using PLupload?(如何使用 PLupload 发送附加数据?)
                change button text in js/ajax after mp4 =gt;mp3 conversion in php(在 php 中的 mp4 =gt;mp3 转换后更改 js/ajax 中的按钮文本)
                  <bdo id='p3kS0'></bdo><ul id='p3kS0'></ul>
                    <tbody id='p3kS0'></tbody>
                  <legend id='p3kS0'><style id='p3kS0'><dir id='p3kS0'><q id='p3kS0'></q></dir></style></legend>

                    1. <small id='p3kS0'></small><noframes id='p3kS0'>

                      <i id='p3kS0'><tr id='p3kS0'><dt id='p3kS0'><q id='p3kS0'><span id='p3kS0'><b id='p3kS0'><form id='p3kS0'><ins id='p3kS0'></ins><ul id='p3kS0'></ul><sub id='p3kS0'></sub></form><legend id='p3kS0'></legend><bdo id='p3kS0'><pre id='p3kS0'><center id='p3kS0'></center></pre></bdo></b><th id='p3kS0'></th></span></q></dt></tr></i><div id='p3kS0'><tfoot id='p3kS0'></tfoot><dl id='p3kS0'><fieldset id='p3kS0'></fieldset></dl></div>
                    2. <tfoot id='p3kS0'></tfoot>