zoltan molnar
zoltan molnar

Reputation: 17

update values from another table with max date and group by user id

table1
|| *webuser_user_id* || *bmi_input_time* || *bmr_index* ||
|| 5 || 2014-07-13 16:28:15 || 2718.00 ||
|| 5 || 2014-07-13 16:42:10 || 2708.00 ||
|| 5 || 2014-07-13 16:50:09 || 2682.00 ||
|| 5 || 2014-07-13 16:50:30 || 2682.00 ||
|| 6 || 2014-07-13 17:13:33 || 3750.00 ||
|| 5 || 2014-07-14 18:11:26 || 2708.00 ||
|| 5 || 2014-07-14 20:13:56 || 2660.00 ||


table2
|| *webuser_user_id* || *user_bmr_value* ||
|| 5 || 0.00 ||
|| 6 || 0.00 ||

i need set the user_bmr_value from table1 from the latest date for each users thanks

Upvotes: 0

Views: 34

Answers (2)

Nebojsa Susic
Nebojsa Susic

Reputation: 1260

Try this query

UPDATE table2 t2
SET user_bmr_value = (SELECT bmr_index 
   FROM table1 t1 
   WHERE t1.webuser_user_id = t2.webuser_user_id 
   ORDER BY bmi_input_time DESC 
   LIMIT 1)

Upvotes: 1

Gordon Linoff
Gordon Linoff

Reputation: 1270553

If I understand correctly, you can do this with a correlated subquery:

update table2 t2
    set t2.user_bmr_value = (select bmr_index
                             from table1 t1
                             where t1.webuser_user_id = t2.webuser_user_id
                             order by bmi_input_time desc
                             limit 1
                            );

Upvotes: 1

Related Questions