sql - MySql UPDATE statement with SELECT summarizing data from table 2 -
i have 2 tables, table has columns token (primary key) , downtime (int
), table b has columns token, status (enum
3 states: active
, unstable
, inactive
), duration (int
).
i want sum duration table b, states unstable
, inactive
. , after that, assign result column downtime, table a.
so, example,
table ======= token downtime -------------------------- bv87pxicnrtk8pw null v3525kq2kzihb9u null table b ======= token state duration ------------------------------------------ v3525kq2kzihb9u active 9 v3525kq2kzihb9u unstable 20 v3525kq2kzihb9u inactive 60 bv87pxicnrtk8pw unstable 11 bv87pxicnrtk8pw active 140 bv87pxicnrtk8pw inactive 40 result ====== token downtime -------------------------- bv87pxicnrtk8pw 51 v3525kq2kzihb9u 80
i tried
update set downtime = (select sum(duration) b state != 'active' group token) a.token = b.token;
but without success.
newbie in sql. can me?
in mysql can use join , update as
update tablea join( select token,sum(duration) duration tableb state != 'active' group token )b on b.token = a.token set a.downtime = b.duration
Comments
Post a Comment