douxia2137 2014-02-15 23:45
浏览 60
已采纳

sql请问减去两个字段[关闭]

I want to ask how to make something like this:

Ive got two tables lets call it A and B. A has a amount_a and in B we have amount_b. I want to ask how to make amount_a - amount_b and then the result asign to amount_a. How can I make it ? All simple examples nice to see. Thank you very much

  • 写回答

2条回答 默认 最新

  • drkug66408 2014-02-15 23:56
    关注

    Very simple example (JOINing on id):

    CREATE two tables with five rows of numbers:

    CREATE TABLE table_a (
       id int auto_increment primary key
     , amount_a int(2)
    );
    
    INSERT INTO table_a (amount_a)
    VALUES (6),(72),(58),(39),(0);
    
    CREATE TABLE table_b (
       id int auto_increment primary key
     , amount_b int(2)
    );
    
    INSERT INTO table_b (amount_b)
    VALUES (1),(23),(31),(14),(51);
    

    Query:

    SELECT
       amount_a - amount_b AS amount
    FROM table_a
    JOIN table_b ON table_a.id = table_b.id
    

    http://sqlfiddle.com/#!2/9b9c6/1

    Results in:

    amount
    5
    49
    27
    25
    -51
    

    And running an UPDATE to amount_a:

    UPDATE table_a
    JOIN table_b ON table_a.id = table_b.id
    SET amount_a = amount_a - amount_b
    

    http://sqlfiddle.com/#!2/0c39e/1

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大