<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)

        SQL Group By and min (MySQL)(SQL Group By 和 min (MySQL))
          <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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  SQL query to group by day(按天分组的 SQL 查询)
                  What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
                  MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
                  MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
                  Include missing months in Group By query(在 Group By 查询中包含缺失的月份)
                  Why Mysql#39;s Group By and Oracle#39;s Group by behaviours are different(为什么 Mysql 的 Group By 和 Oracle 的 Group by 行为不同)
                  <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>

                            <tbody id='mMk8B'></tbody>