如何将字符串 "lat, lon" 转换为 MySql 中的 POINT

How to transform string "lat, lon" into a POINT in MySql

我将 latlon 数据存储在单个 varchar 中,例如“51.51087974,-0.11101941”,我想将其转换为 POINT 空间值。当值位于单独的浮点数中时,我可以使用例如 UPDATE myTable SET coords = GeometryFromText( CONCAT( 'POINT(', lon, ' ', lat, ')' ) ); 我显然可以用 space 替换我的值中的逗号来获取 POINT 数据,但是 lat 和 lon 是错误的方式。我敢肯定这一定是一个常见的操作,但对于我来说似乎无法找到解决方案!

您可以使用mysql的substring_index()函数来解析您的坐标数据:

Returns the substring from string str before count occurrences of the delimiter delim. If count is positive, everything to the left of the final delimiter (counting from the left) is returned. If count is negative, everything to the right of the final delimiter (counting from the right) is returned. SUBSTRING_INDEX() performs a case-sensitive match when searching for delim.

UPDATE myTable 
SET coords = GeometryFromText( CONCAT( 'POINT(', substring_index(coord_field, ',',-1), ' ', substring_index(coord_field, ',',1), ')' ) );