Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add two SQL fields together in new column

Tags:

sql

mysql

I want to add together two numerical values in my database (STOCKVOLUME and UNITS) and insert the result into a different field in a column (NEWVAL). I want this code to do this for every row in the database.

How can I do this?

like image 208
user14377 Avatar asked Nov 08 '12 20:11

user14377


2 Answers

UPDATE YourTable
SET NEWVAL = STOCKVOLUME + UNITS
like image 68
Michael Fredrickson Avatar answered Nov 13 '22 05:11

Michael Fredrickson


If you want to insert into another table :

INSERT INTO aTable(NEWVAL) SELECT (STOCKVOLUME + UNITS) FROM anotherTAble;

If you want to Update another filed of the same table :

UPDATE aTable SET NEWVAL = (STOCKVOLUME + UNITS);
like image 38
aleroot Avatar answered Nov 13 '22 06:11

aleroot