【发布时间】:2015-12-09 10:11:25
【问题描述】:
我在 mysql 中有这个查询:
set @dist=20;
set @earthRadius=6371;
SELECT *,SPLIT_STR(posizione_GPS,';', 1) as mylat,SPLIT_STR(posizione_GPS,';', 2) as mylon,(SELECT CALC_DISTANCE(mylat,mylon,cit.lat,cit.lng,@earthRadius) as distance FROM city as cit HAVING distance<@dist ORDER BY distance asc LIMIT 1 ) as distance FROM statistiche as stat
有了这个,我得到了统计数据,并且鉴于每个统计数据都与一个协调的 GPS 相关联,我想使用我已经在数据库中的表(城市)来获取与之相关联的城市的名称。 计算坐标之间的距离并取距离较近的城市。
CALC_DISTANCE 是一个自定义函数,用于计算两个 gps 点之间的距离。
查询有效,但我需要城市名称,如果我在子查询中放入第二列,名称:
set @dist=20;
set @earthRadius=6371;
SELECT *,SPLIT_STR(posizione_GPS,';', 1) as mylat,SPLIT_STR(posizione_GPS,';', 2) as mylon,(SELECT nome, CALC_DISTANCE(mylat,mylon,cit.lat,cit.lng,@earthRadius) as distance FROM city as cit HAVING distance<@dist ORDER BY distance asc LIMIT 1 ) as distance FROM statistiche as stat
我收到此错误
Error Code: 1241. Operand should contain 1 column(s)
如何获得城市名称? 谢谢
statistiche表的结构是:
`id` int(11) NOT NULL AUTO_INCREMENT,
`utenti_id` int(11) NOT NULL,
`spots_id` int(11) NOT NULL,
`posizione_GPS` varchar(45) DEFAULT NULL,
`data` date DEFAULT NULL,
`ora` time DEFAULT NULL,
PRIMARY KEY (`id`)
city表的结构是:
`id` varchar(10) NOT NULL,
`nome` varchar(100) DEFAULT NULL,
`prov` varchar(45) DEFAULT NULL,
`lat` float(10,6) DEFAULT NULL,
`lng` float(10,6) DEFAULT NULL,
PRIMARY KEY (`id`)
【问题讨论】:
-
CALC_DISTANCE 有效吗?可以手动调用吗?
-
是的工作。这是定义 earth_radius * 2 * ASIN(SQRT( POWER(SIN((mylat - destlat) * pi()/180 / 2), 2) +COS(mylat * pi()/180) * COS(destlat * pi ()/180) * POWER(SIN((mylng -destlng) * pi()/180 / 2), 2) ))
标签: mysql gps subquery city nested-query