Create MySQL spatial column - Point Data type with lat long without using Alter table

冷暖自知 提交于 2019-12-30 14:38:58

问题


How can I create a MySQL spatial column with "Point" Data type using latitude & longitude using a CREATE statement. (Without using Alter)

Do I need to store latitude and longitude as well or can I insert both values into the Point(x,y) field and get rid of the latitude/ longitude columns? The examples that I have read till now retain the lat long fields.

Please share some examples.


回答1:


The point field has both the latitude and longitude data stored inside it and they can be retrieved quite easily if required. Assuming your point field is name pt, the following query gives this information.

SELECT ST_Y(pt), ST_X(pt) FROM my_spatial_table;

This is exactly the same as doing

SELECT Y(pt), X(pt) FROM my_spatial_table;

since X and ST_X are aliases. So in short you only need the point field.

You can add your pt field as follows:

ALTER TABLE my_table ADD COLUMN GEOMETRY;

Then you can move the data from the existing latitude, and longitude columns as follows:

UPDATE my_table SET pt = PointFromText(CONCAT('POINT(',longitude,' ',latitude,')'))

For more details on this please see: https://stackoverflow.com/a/7135890/267540
http://dev.mysql.com/doc/refman/5.7/en/populating-spatial-columns.html



来源:https://stackoverflow.com/questions/39612856/create-mysql-spatial-column-point-data-type-with-lat-long-without-using-alter

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