mysql - Automatically calculate a database field from another table with a foreign key relation -
i have 2 tables:
- table column a-id , double field column total amount,
- table b foreign key column a-id , amount column.
i want total amount in table change automatically, depending on field.
is behavior possible implement? essentially, i'd field hold query runs every time add/delete/update row in table b. i'm using phpmyadmin (if it's relevant).
i've tried using following queries:
- take current total amount , put them in variable named i.
- make changes in table b.
- update new total amount in table a.
however, hasn't been efficient.
don't that. calculate values on-the-fly in single query. need join tables that
select *, a.amount + b.amount total_amount tablea inner join tableb b on a.a_id = b.a_id
Comments
Post a Comment