【发布时间】:2012-02-07 11:31:59
【问题描述】:
自从我接近解决方案后,这已经大大更新了
我想标题不是最好的,但我不知道如何更好地解释它。
我在两个相关表格中有坐标位置。表locations(id, name, description, created_at) 和locations_coordinates(location_id, lat, lng, coordinates_order)。
我正在存储未知数量的坐标(一个多边形),这就是我使用两个表的原因。
现在我正在运行以下查询
SELECT l.id,
l.name,
l.description,
l.created_at,
GROUP_CONCAT(Concat(c.lat, ":", c.lng) ORDER BY c.coordinate_order ASC
SEPARATOR
', ') AS coordinates
FROM locations l
LEFT JOIN locations_coordinates c
ON l.id = c.location_id
WHERE l.id = ' . $id . '
GROUP BY l.id,
l.name,
l.description,
l.created_at ASC
所以我得到以下输出(使用 id = 3):
[
{
"id":"3",
"name":"Stadthalle",
"description":"Die Wiener Stadthalle",
"created_at":"2012-01-07 14:22:06",
"coordinates":"48.201187:16.334213, 48.200665:16.331606, 48.202989:16.331091, 48.203075:16.334192"
}
]
我想得到的是纬度和经度对,比如:
[
{
"id":"3",
"name":"Stadthalle",
"description":"Die Wiener Stadthalle",
"created_at":"2012-01-07 14:22:06",
"coordinates":[
[
"48.201187,16.334213"
],
[
"48.200665,16.331606"
],
[
"48.202989,16.331091"
],
[
"48.203075,16.334192"
]
]
}
]
所以我的问题是:有没有办法只使用 SQL 获得所需的输出?如果没有,是否可以改进我的查询,以便我更轻松地使用应用程序代码(对我来说是 PHP)?
更新:
我正在使用MyISAM,在“locations_coordinates”表中,“locations_id”和“coordinates_order”是PRIMARY,“coordinates_order”设置为AUTO INCREMENT,因此它在插入时总是开始一系列新的订单号。 (我需要这个,所以我可以稍后按正确的顺序选择坐标)。
【问题讨论】: