How to SQL Select a one to many relation and merge the output

不问归期 提交于 2020-01-23 10:49:19

问题


This has been dramatically updated since i got closer to the solution

I guess the title is not the best but I did not know how to explain it better.

I have locations with coordinates in two related tables. Table locations(id, name, description, created_at) and locations_coordinates(location_id, lat, lng, coordinates_order).

I am storing an unknown amount of coordinates (a polygon), that`s why I use two tables.

Now I am running the following query

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 

So i get the following output (using 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"
}
] 

What i would like to get is the latitude and longitude pairs together, something like:

[
{
  "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"
     ]
  ]
}
]

So my question is: is there a way to get the needed output with SQL only? If not, can my query be improved, so that I have it easier doing it with application code (PHP for me) ?

UPDATE:

I`m using MyISAM and in the "locations_coordinates" table, "locations_id" and "coordinates_order" are PRIMARY and "coordinates_order" is set to AUTO INCREMENT, so it starts always a new series of order numbers when inserted. (I need this so i can select the coordinates in the right order later).


回答1:


as long as you setup foreign keys( multiple if need be ), however make sure you index the keys for more speed.

then sure you can do multiple joins like you mention above, as long as locations and coordinates have the foreign key user.id

I answered a similar question a few days ago actually (funny enough it had to do with locations/coordinates ) , unfortunate I cant click on any links tonight to forward you, browse my profile maybe.




回答2:


I resolves similar problems in this way:

First, get all the location records I need, Put them in a hash map with the ID as key.

Second, get all the coordinate records where location_id in location records id. Maybe: select * from coordinate where location_id in (?, ?, ...)

Third, iterate the coordinate records and set lat and lng column into the corresponding location record.

If the size of location records is not too large, it need only two SQL statements. Thus, we get a better performance.



来源:https://stackoverflow.com/questions/8813708/how-to-sql-select-a-one-to-many-relation-and-merge-the-output

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!