我有一个查询,它在一个经纬度/lng坐标对的距离内查找位置。下面是一个填充了lat和lng值的示例:
SELECT
`id`,
( 3959 * acos( cos( radians('39.8657112') ) * cos( radians( `lat` ) ) * cos( radians( `lng` ) - radians('-86.1478729') ) + sin( radians('39.8657112') ) * sin( radians( `lat` ) ) ) ) AS `distance`
FROM `locations`
HAVING `distance` < 0.100000我的问题是,我想在复合查询中使用它来过滤另一个查询的结果。例如:
SELECT * FROM `tickets` WHERE `location_id` IN ( <distance query goes here> );但是,由于距离查询返回两列(id和distance),因此第二个查询会因违反基数而失败:ERROR 1241 (21000): Operand should contain 1 column(s)
有没有什么方法可以让MySQL避免从查询中返回distance列,以便只返回id的列表?
发布于 2013-06-18 05:22:44
典型的响应是构造一个与距离查询相同的查询,但从select子句中删除距离列。
SELECT `id`
FROM `locations`
WHERE ( 3959 * acos( cos( radians('39.8657112') ) * cos( radians( `lat` ) ) * cos( radians( `lng` ) - radians('-86.1478729') ) + sin( radians('39.8657112') ) * sin( radians( `lat` ) ) ) ) < 0.100000您看,我将整个计算移到了WHERE子句中,并针对您的限制器进行了测试。
https://stackoverflow.com/questions/17157066
复制相似问题