douxiao0400 2014-02-14 08:35
浏览 100
已采纳

更新查询中的Mysql类型转换

I have a table with two columns meta_key and meta_value. Both of them are varchar now for meta_key _price the meta_value is a number like 10.0, 20.0 etc. I am trying to update this value to a higher number (percentage increase in price). I am trying to cast meta_value as integer but it is not working.

Here is what I am trying:

update wp_postmeta set CAST(meta_value AS UNSIGNED)=meta_value+meta_value*(0.60) 
where meta_key='_price'

But it gives this error:

error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CAST(meta_value AS UNSIGNED)=CAST'

What I am doing wrong?

  • 写回答

1条回答 默认 最新

  • doulan1866 2014-02-14 08:36
    关注

    Left-side assignment part of UPDATE statement specifies column to update, not the value. You should use CAST() on right side of assignment operation.

    UPDATE
        wp_postmeta
    SET 
        meta_value = CAST(meta_value AS UNSIGNED) * 1.60 
    WHERE
        meta_key = '_price';
    

    Quote from manual:

    UPDATE [LOW_PRIORITY] [IGNORE] table_reference
        SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
        [WHERE where_condition]
        [ORDER BY ...]
        [LIMIT row_count]
    

    The SET clause indicates which columns to modify and the values they should be given.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化