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

        SQL Group By 和 min (MySQL)

        时间:2024-04-16
          <legend id='DI5tq'><style id='DI5tq'><dir id='DI5tq'><q id='DI5tq'></q></dir></style></legend>
        1. <i id='DI5tq'><tr id='DI5tq'><dt id='DI5tq'><q id='DI5tq'><span id='DI5tq'><b id='DI5tq'><form id='DI5tq'><ins id='DI5tq'></ins><ul id='DI5tq'></ul><sub id='DI5tq'></sub></form><legend id='DI5tq'></legend><bdo id='DI5tq'><pre id='DI5tq'><center id='DI5tq'></center></pre></bdo></b><th id='DI5tq'></th></span></q></dt></tr></i><div id='DI5tq'><tfoot id='DI5tq'></tfoot><dl id='DI5tq'><fieldset id='DI5tq'></fieldset></dl></div>
            <tbody id='DI5tq'></tbody>
            <tfoot id='DI5tq'></tfoot>

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

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

                • 本文介绍了SQL Group By 和 min (MySQL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有以下 SQL:

                  select code, distance from places;    
                  

                  输出如下:

                  CODE    DISTANCE            LOCATION
                  106     386.895834130068    New York, NY
                  80      2116.6747774121     Washington, DC
                  80      2117.61925131453    Alexandria, VA
                  106     2563.46708627407    Charlotte, NC
                  

                  我希望能够只获得一个代码和最近的距离.所以我希望它返回这个:

                  I want to be able to just get a single code and the closest distance. So I want it to return this:

                  CODE    DISTANCE            LOCATION
                  106     386.895834130068    New York, NY
                  80      2116.6747774121     Washington, DC
                  

                  我最初有这样的事情:

                  SELECT code, min(distance), location
                  GROUP BY code
                  HAVING distance > 0 
                  ORDER BY distance ASC
                  

                  如果我不想获得与最小距离相关联的正确位置,则 min 工作正常.我如何获得最小(距离)和正确的位置(取决于表格中插入的顺序,有时您最终可能会得到纽约的距离,但最终会得到夏洛特的位置).

                  The min worked fine if I didn't want to get the correct location that was associated with the least distance. How do I get the min(distance) and the correct location (depending on the ordering on the inserts in the table, sometimes you could end up with the New York distance but the Charlotte in Location).

                  推荐答案

                  要获得正确的关联位置,您需要加入一个子选择,该子选择获取每个代码的最小距离,条件是外部主表中的距离与子选择中导出的最小距离匹配.

                  To get the correct associated location, you'll need to join a subselect which gets the minimum distance per code on the condition that the distance in the outer main table matches with the minimum distance derived in the subselect.

                  SELECT a.code, a.distance
                  FROM   places a
                  INNER JOIN
                  (
                      SELECT   code, MIN(distance) AS mindistance
                      FROM     places
                      GROUP BY code
                  ) b ON a.code = b.code AND a.distance = b.mindistance
                  ORDER BY a.distance
                  

                  这篇关于SQL Group By 和 min (MySQL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用 mysql group by 返回计数 0 下一篇:将表格分组为 15 分钟间隔

                  相关文章

                  <tfoot id='mMk8B'></tfoot>
                • <legend id='mMk8B'><style id='mMk8B'><dir id='mMk8B'><q id='mMk8B'></q></dir></style></legend>

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

                  • <bdo id='mMk8B'></bdo><ul id='mMk8B'></ul>

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