我在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`)