Sum total of several MySQL columns stored in another column?

前端 未结 4 924
独厮守ぢ
独厮守ぢ 2021-01-20 13:27

I have several columns in a MySQL database that I would like to add and store in another column:

column1     column2     column3    subtotal
10          10           


        
4条回答
  •  独厮守ぢ
    2021-01-20 13:58

    How about this?

    UPDATE tableName SET subtotal = (column1 + column2 + column3)
    

    Update 1

    If you really don't need subtotal column in table just use query as

    SELECT *, (column1 + column2 + column3) as subtotal
    FROM tableName
    

    Demo

提交回复
热议问题