• <tfoot id='OYh9B'></tfoot>

        <legend id='OYh9B'><style id='OYh9B'><dir id='OYh9B'><q id='OYh9B'></q></dir></style></legend>
          <bdo id='OYh9B'></bdo><ul id='OYh9B'></ul>
      1. <small id='OYh9B'></small><noframes id='OYh9B'>

      2. <i id='OYh9B'><tr id='OYh9B'><dt id='OYh9B'><q id='OYh9B'><span id='OYh9B'><b id='OYh9B'><form id='OYh9B'><ins id='OYh9B'></ins><ul id='OYh9B'></ul><sub id='OYh9B'></sub></form><legend id='OYh9B'></legend><bdo id='OYh9B'><pre id='OYh9B'><center id='OYh9B'></center></pre></bdo></b><th id='OYh9B'></th></span></q></dt></tr></i><div id='OYh9B'><tfoot id='OYh9B'></tfoot><dl id='OYh9B'><fieldset id='OYh9B'></fieldset></dl></div>
      3. SQL 计算所有行而不是计算单个行

        时间:2023-08-18
          <tbody id='eQIE0'></tbody>

            <bdo id='eQIE0'></bdo><ul id='eQIE0'></ul>
            • <tfoot id='eQIE0'></tfoot>
              1. <small id='eQIE0'></small><noframes id='eQIE0'>

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

                  本文介绍了SQL 计算所有行而不是计算单个行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个从数据库请求数据的 SQL 语句.

                  I have a SQL statement which requests data from the database.

                  SELECT `ID`, `To`, `Poster`, `Content`, `Time`, ifnull(`Aura`,0) as `Aura` FROM (
                      SELECT * FROM (
                          SELECT DISTINCT * FROM messages m
                          INNER JOIN
                          (
                              SELECT Friend2 as Friend FROM friends WHERE Friend1 = '1'
                              UNION ALL
                              SELECT Friend1 as Friend FROM friends WHERE Friend2 = '1'
                          ) friends ON m.Poster = friends.`Friend`
                          UNION ALL SELECT DISTINCT *, '1' FROM messages where `Poster`='1'
                      ) var
                      LEFT JOIN
                      (
                          select `ID` as `AuraID`, `Status` as `AuraStatus`, count(*) as `Aura`
                              from messages_aura
                      ) aura ON (var.Poster = aura.AuraID AND var.ID = aura.AuraStatus)
                  ) final
                  
                  GROUP BY `ID`, `Poster`
                  ORDER BY `Time` DESC LIMIT 10
                  

                  这是我的 messages_aura 表格布局.它显示 IDStatusUserID.

                  Here is my messages_aura table layout. It shows ID, Status and UserID.

                  这是上述语句的输出.

                  (上面截图中的ID指的是下面的Poster,上面截图中的Status指的是ID代码> 下面)

                  (The ID from the above screenshot refers to Poster below and the Status from the above screenshot refers to ID below)

                  该语句应为底行提供 1 的 Aura 计数,并为顶行提供 2 的 Aura 计数.怎么了?

                  The statement should give the bottom row a Aura count of 1 and the top row an Aura count of 2. What's wrong?

                  推荐答案

                  您缺少 GROUP BY,因此它会计算所有内容,而不是按某些列分组.

                  You're missing GROUP BY, so it's counting everything instead of grouping by some columns.

                  LEFT JOIN
                  (
                      select `ID` as `AuraID`, `Status` as `AuraStatus`, count(*) as `Aura`
                      from messages_aura
                      GROUP BY AuraID, AuraStatus
                  ) aura ON (var.Poster = aura.AuraID AND var.ID = aura.AuraStatus)
                  

                  这篇关于SQL 计算所有行而不是计算单个行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:返回 IN 值的默认结果,不管 下一篇:MySQL 中 INDEX、PRIMARY、UNIQUE、FULLTEXT 之间的区别

                  相关文章

                  最新文章

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

                    <small id='nYRvb'></small><noframes id='nYRvb'>

                        <bdo id='nYRvb'></bdo><ul id='nYRvb'></ul>