У меня есть две таблицы,
Национальная широта, долгота (italy_db) => id | шапка | Comune | широтный | продольный
Список магазинов БД (в центре) => id_geo | адрес | лат | lng
CREATE TABLE `italy_db` (
`id` int(11) NOT NULL,
`cap` varchar(5) DEFAULT NULL,
`comune` varchar(48) DEFAULT NULL,
`latitudine` decimal(13,9) DEFAULT NULL,
`longitudine` decimal(13,9) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `center` (
`id_geo` int(11) NOT NULL,
`address` varchar(50) NOT NULL,
`lat` varchar(255) NOT NULL,
`lng` varchar(255) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `italy_db` (`id`, `cap`, `comune`, `latitudine`, `longitudine`) VALUES
(145, '00137', 'Roma', '41.952211560', '12.565102860'),
(146, '00138', 'Roma', '42.005710820', '12.522652930'),
(147, '00139', 'Roma', '41.990002960', '12.557905280');
INSERT INTO `center` (`id_geo`, `address`, `lat`, `lng`) VALUES
(1, 'Via Luigi Corsi', '44.3060039', '8.4779466'),
(2, 'Via Fieschi', '44.4062547', '8.9348863'),
(3, 'Corso Garibaldi', '44.3153768', '9.3244363');
Такой запрос, но без UNION SELECT + широта и долгота от italy_db:
SELECT (TRUNCATE(6371 * acos(cos(radians(41.952211560))
* cos(radians(lat))
* cos(radians(lng) - radians(12.565102860))
+ sin(radians(41.952211560))
* sin(radians(lat))), 2)) AS distance, address
FROM center
UNION
SELECT (TRUNCATE(6371 * acos(cos(radians(42.005710820))
* cos(radians(lat))
* cos(radians(lng) - radians(12.522652930))
+ sin(radians(42.005710820))
* sin(radians(lat))), 2)) AS distance, address
FROM center
UNION
SELECT (TRUNCATE(6371 * acos(cos(radians(41.990002960))
* cos(radians(lat))
* cos(radians(lng) - radians(12.557905280))
+ sin(radians(41.990002960))
* sin(radians(lat))), 2)) AS distance, address
FROM center
UNION...
Желаемый результат это что-то вроде этого:
+-----+----------+-----------------+
| id | distance | address |
+-----+----------+-----------------+
| 145 | 422.41 | Via Luigi Corsi |
| 145 | 401.3 | Via Fieschi |
| 145 | 371.69 | Corso Garibaldi |
| 146 | 415.92 | Via Luigi Corsi |
| 146 | 394.65 | Via Fieschi |
| 146 | 364.97 | Corso Garibaldi |
| 147 | 419.28 | Via Luigi Corsi |
| 147 | 397.96 | Via Fieschi |
| 147 | 368.26 | Corso Garibaldi |
+-----+----------+-----------------+
http://sqlfiddle.com/#! 9 / 811e4b / 1
спасибо!