mysql - 如何在mysql中将字符串转换为 float ?

我有一个表,其中包含存储为字符串 (VARCHAR) 的纬度和经度值,我想将其转换为 FLOAT (10,6)

但是,使用 CAST()CONVERT() 似乎没有一种直接的方法。

如何轻松转换这些列?这是一次性转换。

最佳答案

原来我只是在 CAST() 描述中遗漏了 DECIMAL:

DECIMAL[(M[,D])]

Converts a value to DECIMAL data type. The optional arguments M and D specify the precision (M specifies the total number of digits) and the scale (D specifies the number of digits after the decimal point) of the decimal value. The default precision is two digits after the decimal point.

因此,以下查询有效:

UPDATE table SET
latitude = CAST(old_latitude AS DECIMAL(10,6)),
longitude = CAST(old_longitude AS DECIMAL(10,6));

https://stackoverflow.com/questions/7368163/

相关文章:

php - MySQL 删除多列

mysql - 如何在 SQL 查询中对 LEFT JOIN 的顺序进行排序?

mysql - 列计数与第 1 行的值计数不匹配

mysql - 最好的 MySQL 性能调优工具?

php - 从字段不匹配条件的表中选择

php - PDO fetchAll 将键值对分组到 assoc 数组中

mysql - 设计用户角色和权限系统的最佳实践?

php - DateTime 类的对象无法转换为字符串

mysql - INSERT ... 使用 WHERE 进行重复的 key 更新?

php - 如何使用 Laravel 4 Eloquent 连接列?