Sum total of several MySQL columns stored in another column?

前端 未结 4 888
独厮守ぢ
独厮守ぢ 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:53

    Well if this is just a one time deal you could do an update query like so:

    UPDATE MyTable set subtotal = (column1 + column2 + column3)
    

    If you want it to be calculated on insert, you could have a calculated column.

    I would personally just wait until you need it to calculate it so that information that could easily be derived from your data isn't bloating your DB.

提交回复
热议问题