Php MySQL-组合两个查询
我无法解决这个问题: 我有很多地理坐标的mysql数据库 我想知道哪些点在特定坐标和距离的特定英里数内 在特定的正方形内 特定区域内的点:Php MySQL-组合两个查询,php,sql,union,Php,Sql,Union,我无法解决这个问题: 我有很多地理坐标的mysql数据库 我想知道哪些点在特定坐标和距离的特定英里数内 在特定的正方形内 特定区域内的点: (SELECT *, 'area' as type, ( 3959 * acos( cos( radians(".$Plat.") ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(".$Plng.") ) + sin( radians(
(SELECT
*,
'area' as type,
( 3959 * acos( cos( radians(".$Plat.") ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(".$Plng.") ) + sin( radians(".$lat.") ) * sin( radians( lat ) ) ) ) AS distance
FROM
".$db."
HAVING
distance < ".$radius."
- id(72)仅存在于区域A中,而不存在于正方形中李>
- id(81)仅存在于正方形中,而不存在于该区域中
- id(42)在该区域内,也在广场内。所以它出现了两次。但它应该只有一次。该地区应该得到优先考虑
+------+-----------+-----------+----------+-------------+
| id | Alat | Alng | distance | type |
+------+-----------+-----------+----------+-------------+
| 42 | 53.704678 | 10.202164 | 12345 | area
+------+-----------+-----------+------------------------+
| 72 | 23.704678 | 15.202164 | 12345 | area
+------+-----------+-----------+------------------------+
......
+------+-----------+-----------+------------------------+
| 81 | 43.778 | 15.201212 | -1 | square
+------+-----------+-----------+------------------------+
......
在联合后使用GROUP BY删除重复项
SELECT id, Alat, Alng, distance, type
FROM (
SELECT *, 'area' as type, ... FROM ...
UNION ALL
SELECT *, 'square' as type, ... FROM ...
) AS subq
GROUP BY id, Alat, Alng, distance
对于较大的结果集,这可能不是很有效,但应该有效
请注意,在SQL标准和许多其他数据库中禁止使用这种分组方式(因为
type
不在分组方式中),但MySQL允许使用这种方式。请以表格形式提供示例数据和预期结果。
+------+-----------+-----------+----------+-------------+
| id | Alat | Alng | distance | type |
+------+-----------+-----------+----------+-------------+
| 42 | 53.704678 | 10.202164 | 12345 | area
+------+-----------+-----------+------------------------+
| 72 | 23.704678 | 15.202164 | 12345 | area
+------+-----------+-----------+------------------------+
......
+------+-----------+-----------+------------------------+
| 81 | 43.778 | 15.201212 | -1 | square
+------+-----------+-----------+------------------------+
......
SELECT id, Alat, Alng, distance, type
FROM (
SELECT *, 'area' as type, ... FROM ...
UNION ALL
SELECT *, 'square' as type, ... FROM ...
) AS subq
GROUP BY id, Alat, Alng, distance