开发者社区> 问答> 正文

MYSQL更新,字段值等于上一条字段值加上现有字段值如何实现?

已解决

one等于上一个one加上现在的money,这个mysql语句怎么写?
9

展开
收起
落地花开啦 2016-01-31 14:52:17 4434 0
1 条回答
写回答
取消 提交回答
  • 公益是一辈子的事, I am digoal, just do it. 阿里云数据库团队, 擅长PolarDB, PostgreSQL, DuckDB, ADB等, 长期致力于推动开源数据库技术、生态在中国的发展与开源产业人才培养. 曾荣获阿里巴巴麒麟布道师称号、2018届OSCAR开源尖峰人物.
    采纳回答

    演示一下PostgreSQL的用法:
    需要用到递归查询。
    mysql好像不支持。

    postgres=# create table m(id serial primary key,money int, one int);
    CREATE TABLE
    postgres=# insert into m(money,one) values (0,2000),(85,0),(100,0),(19,0),(21,0);
    INSERT 0 5
    postgres=# select * from m;
     id | money | one  
    ----+-------+------
      1 |     0 | 2000
      2 |    85 |    0
      3 |   100 |    0
      4 |    19 |    0
      5 |    21 |    0
    (5 rows)
    
    postgres=# with recursive t(id,money,one) as (select 1 id,0 money,2000 one union all select t1.id,t1.money,t.one-t1.money one from t,m t1 where t.id=t1.id-1) select * from t;
     id | money | one  
    ----+-------+------
      1 |     0 | 2000
      2 |    85 | 1915
      3 |   100 | 1815
      4 |    19 | 1796
      5 |    21 | 1775
    (5 rows)
    2019-07-17 18:36:43
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
搭建电商项目架构连接MySQL 立即下载
搭建4层电商项目架构,实战连接MySQL 立即下载
PolarDB MySQL引擎重磅功能及产品能力盛大发布 立即下载

相关镜像