我的桌子上摆满了人们的位置。我想让最亲近的人相互匹配。一个人不能匹配两次。
我有第一部分,但我无法弄清楚如何避免两次匹配同一个人。
表:
CREATE TABLE `_person` (
`id_person` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`lat` decimal(30,28) NOT NULL,
`long` decimal(30,27) NOT NULL,
PRIMARY KEY (`id_person`)
) ENGINE=InnoDB AUTO_INCREMENT=351 DEFAULT CHARSET=latin1;
选择
SELECT a.id_person, a.name, b.id_person, b.name, 111.1111 *
DEGREES(ACOS(COS(RADIANS(a.lat))
* COS(RADIANS(b.lat))
* COS(RADIANS(a.long - b.long))
+ SIN(RADIANS(a.lat))
* SIN(RADIANS(b.lat)))) AS distance_in_km,
CONCAT(a.id_person, '_', b.id_person) AS match_id
FROM _person a CROSS JOIN _person b
WHERE a.id_person < b.id_person;
参与:
id name id name distance_in_km match_id
157 Rubin Kouba 192 Francie Lansford 1.3349701041430388 157_192
122 Pearly Godin 157 Rubin Kouba 7.621880803034831 122_157
122 Pearly Godin 192 Francie Lansford 8.604980125095778 122_192
18 Morris Haines 70 Jenelle Eurich 28.469537174712613 18_70
97 Fabiola Lister 122 Pearly Godin 32.29419525788823 97_122
97 Fabiola Lister 157 Rubin Kouba 37.710767037180126 97_157
97 Fabiola Lister 192 Francie Lansford 39.040035483275396 97_192
201 Chastity Clonts 334 Glinda Rosch 59.90641505745896 201_334
Rubin Kouba已在第一行匹配,不应在第二行匹配。
这可能在(我的)sql中吗?
谢谢