简体   繁体   English

使用最大日期和按用户ID分组更新另一个表中的值

[英]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 我需要从每个用户的最新日期开始,从table1设置user_bmr_value,谢谢

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
                            );

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM