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

前端 未结 1 461
走了就别回头了
走了就别回头了 2021-01-17 06:16

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 la

相关标签:
1条回答
  • 2021-01-17 06:58

    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

    0 讨论(0)
提交回复
热议问题