I'm better at design but i need to do this and cant figure it out how to do it so ill explain the problem:
So I have this table:
And this one:
I want to do is to calculate the sum of the iliquido
from the 1st table and put the result at total_iliquido
form the 2nd table where id_proposta
is equal in both tables;
Take the values from total_iliquido
and calculate with desconto
(discount) and put the value on subtotal;
Take subtotal value and calculate with iva and put the result in total;
And that it can someone explain me how to do it please, really need your help guys. So as required here it is the Struct of database:
1st table:
2nd table:
And the connections I made:
So thats all i hope it help.
I didn't test this, but you might get the idea.
UPDATE table2 t2
INNER JOIN(
SELECT id_proposta, SUM(iliquido) as total
FROM table1
GROUP BY id_proposta
) x ON table2.id_proposta = table1.id_proposta
SET table2.total_iliquido = x.total,
table2.subtotal = table2.total_iliquido-table2.discount,
table2.total=table2.subtotal+table2.iva
EDIT:
To execute this query using php, you can use the following code:
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Fail: ' . mysql_error());
}
echo 'Success';
$query = "...";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
mysql_close($link);
?>
UPDATE propostas t2
INNER JOIN(
SELECT id_proposta, SUM(iliquido) as total
FROM proposta_print
GROUP BY id_proposta
) x ON t2.id_proposta = x.id_proposta
SET t2.total_iliquido = x.total,
t2.subtotal = t2.total_iliquido*t2.desconto/100,
t2.total = (t2.subtotal*t2.iva/100)+t2.subtotal
A fix for the one on the top, Thanks for your help Stefan Manciu.