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

      <bdo id='2J82U'></bdo><ul id='2J82U'></ul>

    <small id='2J82U'></small><noframes id='2J82U'>

  2. <tfoot id='2J82U'></tfoot>

      如何在MySql中返回具有相同列值的行

      时间:2023-08-20

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

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

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

              • 本文介绍了如何在MySql中返回具有相同列值的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                让我们考虑下表-

                ID Score
                1  95
                
                2  100
                
                3  88
                
                4  100
                
                5  73
                

                我是一个完全的 SQL 菜鸟,但如何返回具有 ID 2 和 4 的分数?所以它应该返回 100,因为它同时出现在 ID 2 和 4

                I am a total SQL noob but how do I return the Scores featuring both IDs 2 and 4? So it should return 100 since its featured in both ID 2 and 4

                推荐答案

                这是一个sets-within-sets"查询示例.我建议使用 have 子句进行聚合,因为这是最灵活的方法.

                This is an example of a "sets-within-sets" query. I recommend aggregation with the having clause, because it is the most flexible approach.

                select score
                from t
                group by score
                having sum(id = 2) > 0 and -- has id = 2
                       sum(id = 4) > 0     -- has id = 4
                

                这是按分数聚合的.然后 have 子句的第一部分 (sum(id = 2)) 计算每个分数有多少个2".第二个是计算有多少个4".仅返回具有2"和4"的分数.

                What this is doing is aggregating by score. Then the first part of the having clause (sum(id = 2)) is counting up how many "2"s there are per score. The second is counting up how many "4"s. Only scores that have at a "2" and "4" are returned.

                这篇关于如何在MySql中返回具有相同列值的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何在 MySQL 中重置 AUTO_INCREMENT 下一篇:从一个表中查找另一个表中不存在的记录

                相关文章

                最新文章

              • <small id='xk0WM'></small><noframes id='xk0WM'>

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

                    <tfoot id='xk0WM'></tfoot>