问题
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