1

我有以下 SQL:

select code, distance from places;    

输出如下:

CODE      DISTANCE
106     386.895834130068
80      2116.6747774121 
80      2117.61925131453
106     2563.46708627407

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

CODE      DISTANCE
106     386.895834130068
80      2116.6747774121

仍在使用 Distinct 和 Group By。

4

5 回答 5

3
select `code`, min(distance) as minimal_distance
from places
group by `code`
于 2012-07-18T07:57:18.860 回答
2
select code, min(distance) as distance
from places
group by code

这里有 GROUP BY 的手册:http: //dev.mysql.com/doc/refman/5.0/en/group-by-functions-and-modifiers.html

教程:

于 2012-07-18T07:58:41.653 回答
2

试试这个:

SELECT
  Code,
  MIN(Distance) AS closest_distance
FROM
  your_table
GROUP BY
  Code
于 2012-07-18T07:59:42.633 回答
2
select `code`, min(distance) as distance
from places
group by `code`
于 2012-07-18T08:00:34.207 回答
1
select code, min(distance) as mindist
from places
group by code
于 2012-07-18T07:57:41.777 回答