【发布时间】:2020-05-15 21:14:17
【问题描述】:
我需要从按距离按 store_id 顺序分组的分支表中选择所有分支
换句话说,我需要找到每个商店最近的分店
select distinct `branch_id`, `store_id`, `branch_name`, distance
from `store_branches`
groupBy store_id having MIN(distance)
orderBy `distance` asc
MIN(distance) 使查询返回空结果
我使用了因为 orderBy 没有做这项工作,因为 groupBy 在 orderBy 之前应用了
【问题讨论】:
-
拥有
MIN()是什么? -
MIN 距离:距离的最小值
-
HAVING表示与您期望的不同。这是像WHERE这样的条件。您要求的是具有非零最小值的东西,但您不在乎什么。
标签: mysql