Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiplying two columns of same table and storing result to the third column of same table

I m having a table

SALES(sno,comp_name,quantity,costperunit,totalcost)

After supplying the costperunit values, totalcost need to be calculated as "totalcost=quantity*costperunit".

I want to multiply 'quantity' and 'costperunit' columns and store the result in 'totalcost' column of same table.

I have tried this:

insert into SALES(totalcost) select quantity*costperunit as res from SALES

But It failed!

Somebody please help me in achieving this.. Thanks in Advance

like image 259
rtvalluri Avatar asked Jul 17 '13 10:07

rtvalluri


People also ask

How do you multiply two columns from the same table in SQL?

All you need to do is use the multiplication operator (*) between the two multiplicand columns ( price * quantity ) in a simple SELECT query. You can give this result an alias with the AS keyword; in our example, we gave the multiplication column an alias of total_price .

How do I multiply values based on criteria in another column in Excel?

For example, to multiply values in columns B, C and D, use one of the following formulas: Multiplication operator: =A2*B2*C2. PRODUCT function: =PRODUCT(A2:C2) Array formula (Ctrl + Shift + Enter): =A2:A5*B2:B5*C2:C5.


2 Answers

Try updating the table

UPDATE SALES SET totalcost=quantity*costperunit
like image 87
TechDo Avatar answered Oct 23 '22 12:10

TechDo


You need to use update.

UPDATE SALES SET totalcost=quantity*costperunit
like image 40
Rohan Avatar answered Oct 23 '22 13:10

Rohan