有人告诉我要针对一个显然是重复的问题提出一个新问题,如果重复没有给我我想要的正确答案。
因此,我系统的基础是我有一个填充有“房屋”位置/坐标的数据库。我进行了高级查询,选择了某个坐标附近的所有房屋,就像这样;
SELECT id, type, distance FROM (SELECT b.id, b.type, b.x, b.y, b.z,
SQRT(POWER(ABS(1654.5413 - b.x), 2) + POWER(ABS(-2293.7571 - b.y), 2) + POWER(ABS(-1.1996 - b.z), 2)) AS "distance"
FROM businesses b ORDER BY distance ASC) as T;
这将导致;
+------+------+------------------------+
| id | type | distance |
+------+------+------------------------+
| 1953 | 2 | 0.00004489639611771451 |
| 2 | 100 | 8.757256937390904 |
| 1959 | 2 | 8.999959765646956 |
| 1960 | 2 | 10.499959765643807 |
| 1961 | 2 | 11.999959765641446 |
| 1962 | 2 | 13.499959765639607 |
| 1963 | 2 | 14.999959765638138 |
| 1964 | 2 | 16.499959765636934 |
| 2055 | 3 | 17.11486010149676 |
| 2054 | 1 | 17.751048488860313 |
| 1965 | 2 | 17.999959765635932 |
| 1966 | 2 | 19.499959765635083 |
| 1967 | 2 | 20.999959765634358 |
| 2056 | 5 | 22.26658275782834 |
| 1968 | 2 | 22.499959765633726 |
| 1969 | 2 | 23.999959765633175 |
| 2057 | 5 | 24.054132659013334 |
| 1970 | 2 | 25.49995976563269 |
| 2058 | 5 | 26.001138245767084 |
| 2061 | 4 | 26.853239370669378 |
| 1971 | 2 | 26.99995976563226 |
| 1972 | 2 | 28.49995976563187 |
| 2060 | 5 | 28.55999771765475 |
| 1973 | 2 | 29.999959765631523 |
| 2059 | 5 | 31.414688663981224 |
| 1974 | 2 | 31.499959765631207 |
| 1 | 100 | 121468.4587678613 |
+------+------+------------------------+
我基本上想要做的是抓住距离最短的行,但是我不希望基于“类型”列重复行,就像这样;
+------+------+------------------------+
| id | type | distance |
+------+------+------------------------+
| 1953 | 2 | 0.00004489639611771451 |
| 2 | 100 | 8.757256937390904 |
| 2055 | 3 | 17.11486010149676 |
| 2054 | 1 | 17.751048488860313 |
| 2056 | 5 | 22.26658275782834 |
| 2061 | 4 | 26.853239370669378 |
+------+------+------------------------+
在上一个问题中我收到了两个答案(都错了),它们都是正确的一半,其中一个答案是;
“使用嵌套查询更简单:
SELECT id,type, MIN(distance) AS 'minDistance'
FROM(
SELECT id, type, distance FROM (SELECT b.id, b.type, b.x, b.y, b.z,
SQRT(POWER(ABS(1654.5413 - b.x), 2) + POWER(ABS(-2293.7571 - b.y), 2) + POWER(ABS(-1.1996 - b.z), 2)) AS "distance"
FROM businesses b ORDER BY distance ASC) as T) AS TABLE
GROUP BY type
ORDER BY 'minDistance'
”
^上面的答案确实选择了我期望的正确距离。 但是它没有做的是匹配id字段和我要选择的所有其他字段,基本上使具有不同值的行不匹配。不知道我是否正确解释了,但是基本上就是这样。
如果我的表中填充了这些行;
+------+------+------------------------+
| id | type | distance |
+------+------+------------------------+
| 1960 | 2 | 65.757256937390904 |
| 1953 | 2 | 0.00004489639611771451 |
| 2055 | 2 | 17.11486010149676 |
+------+------+------------------------+
然后运行从答案中得到的查询,我会得到这个;
+------+------+------------------------+
| id | type | distance |
+------+------+------------------------+
| 1960 | 2 | 0.00004489639611771451 |
+------+------+------------------------+
但是正如您所看到的,该距离与正确的id字段不对应,应该是1953年的id,而是1960年的。
我将如何继续进行纠正?
PS;如果您对其他主题/问题感兴趣; select non duplicates by column from select query
答案 0 :(得分:1)
MySql 8兼容
WITH dist as (
SELECT b.id, b.type, b.x, b.y, b.z,
SQRT(POWER(ABS(1654.5413 - b.x), 2) + POWER(ABS(-2293.7571 - b.y), 2) + POWER(ABS(-1.1996 - b.z), 2)) AS "distance"
FROM businesses b
)
select * from dist d1
where distance = (
select min(distance)
from dist d2
where d1.type = d2.type
)
对于Mysql 8 <,您可以考虑为查询创建视图,例如
CREATE VIEW DIST AS (
SELECT b.id, b.type, b.x, b.y, b.z,
SQRT(POWER(ABS(1654.5413 - b.x), 2) + POWER(ABS(-2293.7571 - b.y), 2) + POWER(ABS(-1.1996 - b.z), 2)) AS "distance"
FROM businesses b
)
然后
select * from dist d1
where distance = (
select min(distance)
from dist d2
where d1.type = d2.type
)
答案 1 :(得分:0)
只需要一个hading子句
SELECT id,type, distance AS 'minDistance'
FROM(
SELECT id, type, distance FROM (SELECT b.id, b.type, b.x, b.y, b.z,
SQRT(POWER(ABS(1654.5413 - b.x), 2) + POWER(ABS(-2293.7571 - b.y), 2) + POWER(ABS(-1.1996 - b.z), 2)) AS "distance"
FROM businesses b ORDER BY distance ASC) as T) AS TABLE
GROUP BY type having distance=min(distance) -- added
ORDER BY 'minDistance'