'subtract two column in mysql
I have 3 column in my database . column a column b column c. I want subtract column a and b then input result in column c. I use PHP and mysqli. if you have sample source I'm happy to introduce or advise how to write
Solution 1:[1]
Insert into table_name (column_c)
select column_a - column_b from table_name;
Solution 2:[2]
I know this is super old, but since it was the top result I got, here's a way forward for anyone else that's looking: check out generated columns.
A decent tutorial is here, and a quick google will get you plenty more on the topic.
For the question asked, the benefit of a generated column is that the math being done (subtraction, in this case) is done automatically by the database.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Priyanshu |
Solution 2 | jetsetter |