【问题标题】:split float value in two columns of mysql database在mysql数据库的两列中拆分浮点值
【发布时间】:2014-03-17 10:36:48
【问题描述】:

我以这种格式在“VARCHAR”类型的一列中错误地输入了 2 个值 value1.value2(浮点值格式)例如 5.10、4.0、3.11 等

现在我想将这两个值分成两个单独的列,类型为“INT” 我已经创建了列,我只需要一个查询。

我不知道如何编写查询来拆分一列的值并更新其他列。 有谁知道,请帮忙。

谢谢

【问题讨论】:

    标签: mysql sql


    【解决方案1】:

    这在 MySQL 中很容易做到:

    select cast(substring_index(varcharcol, '.', 1) as unsigned) as val1,
           cast(substring_index(varcharcol, '.', -1) as unsigned) as val2
    

    编辑:

    如果您愿意,也可以在update 中执行此操作:

    update table t
        set val1 = cast(substring_index(varcharcol, '.', 1) as unsigned),
            val2 = cast(substring_index(varcharcol, '.', -1) as unsigned)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-15
      • 2018-10-02
      • 2019-05-26
      相关资源
      最近更新 更多