私は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 は、2 つの GPS ポイント間の距離を計算するためのカスタム関数です。
クエリは機能しますが、都市の名前が必要です。サブクエリに 2 番目の列を入力すると、次のようになります。
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`)