php - How to update one table based on another one? -
i have these 2 tables:
// user +----+-------+------------+ | id |  name | total_rep  | +----+-------+------------+ | 1  | jack  | 100        | | 2  | peter | 334        | | 3  | john  | 1          | | 4  | ali   | 5463       | +----+-------+------------+  // rep +----+------------+---------+------+ | id | reputation | id_user | done | +----+------------+---------+------+ | 1  | 5          | 2       | 1    | | 2  | 2          | 3       | 1    | | 3  | 15         | 2       | null | | 4  | 10         | 2       | null | | 5  | 5          | 4       | 1    | | 6  | 10         | 3       | null | +----+------------+---------+------+ i'm trying sum number of reputation column rep table done null specific user , add total_rep column user table. expected output:
// specific user $id = 2;  // user +----+-------+------------+ | id |  name | total_rep  | +----+-------+------------+ | 1  | jack  | 100        | | 2  | peter | 359        | -- updated | 3  | john  | 1          | | 4  | ali   | 5463       | +----+-------+------------+ note: update done column , set null values user 1. (this not question, can myself)
how can that?
one way use result of subquery scalar value in update statement.
update `user`  set total_rep = total_rep + (     select sum(reputation) rep_sum `rep` done null , id_user = 2) id = 2;