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:

  1. take current total amount , put them in variable named i.
  2. make changes in table b.
  3. 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

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -