[英]Limit distinct values in MySQL
I'm building something extremely similar to a "store locator" app. 我正在构建一个与“商店定位器”应用程序非常相似的东西。 This queries by lat and lon and produces the nearest locations within a radius.
这通过lat和lon进行查询并生成半径内最近的位置。 This is working perfectly fine, but my database includes locations and sub-locations.
这工作得很好,但我的数据库包括位置和子位置。 I would like to be able to limit the distinct results the users get by location, while still querying and obtaining all information about the sub-location..
我希望能够限制用户按位置获得的不同结果,同时仍然查询并获取有关子位置的所有信息。
Say this is my result set before the distinct selection is applied: 在应用不同选择之前说这是我的结果集:
| Location | Sub-Location |
+----------+--------------+
| Alpha | Alpha North |
| Alpha | Alpha East |
| Alpha | Alpha West |
| Beta | Beta West |
| Gamma | Gamma North |
| Gamma | Gamma South |
| Delta | Delta West |
| Delta | Delta West 2 |
I need a way to can specify a range - let's say 2 - and produce the following result set: 我需要一种方法来指定范围 - 比方说2 - 并生成以下结果集:
| Location | Sub-Location |
+----------+--------------+
| Alpha | Alpha North |
| Alpha | Alpha East |
| Alpha | Alpha West |
| Beta | Beta West |
This would be the effective equivalent of producing the "two nearest" locations to the user. 这将是向用户生成“最近的两个”位置的有效等价物。 After a length of Googling and scouring Stack Overflow's suggestions and similar questions, I can't find anything that fits this description.
经过一段时间的谷歌搜索和搜索Stack Overflow的建议和类似的问题,我找不到符合此描述的任何内容。
Would someone mind pointing me in the right direction, or perhaps provide a query example that can do this? 有人会介意我指出正确的方向,或者提供一个可以做到这一点的查询示例吗?
EDIT: This is the query I'm running with. 编辑:这是我正在运行的查询。 It's a bit of a monster;
这有点像怪物; includes the lat/lon query as well.
还包括lat / lon查询。
SELECT *
FROM(
(SELECT * FROM locationstable JOIN
(SELECT DISTINCT location,
( 3959 * acos( cos( radians(47.4972680) ) * cos( radians( lat ) )
* cos( radians( lon ) - radians(-122.2564740) ) + sin( radians( 47.4972680) )
* sin( radians( lat ) ) ) ) AS distance
FROM locationstable
HAVING distance < 1
LIMIT 0, 5)
locationstable ON locationstable.location = location
)) locationstable
LEFT JOIN informations
ON substring(locationstable.locationsublocation, 4) = informations.storeinformations
子查询怎么样?
SELECT * FROM sometable WHERE Location IN (SELECT DISTINCT Location FROM sometable LIMIT 2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.