MySQL: Update table column from subquery result

Posted by Jhourlad Estrella on Stack Overflow See other posts from Stack Overflow or by Jhourlad Estrella
Published on 2010-06-08T08:20:38Z Indexed on 2010/06/13 21:22 UTC
Read the original article Hit count: 156

Filed under:

On the Members table are columns "MemberID" and "PointsEarned".

I want to update the PointsEarned column from the result of this query:

SELECT m.MemberID, m.UserName, 
     ( (SELECT COUNT(*) FROM EventsLog as e WHERE e.MemberID=m.MemberID AND e.EventsTypeID=2)*10 ) + 
     ( (SELECT COUNT(*) FROM EventsLog as e WHERE e.MemberID=m.MemberID AND e.EventsTypeID=3)*3 ) +
     ( (SELECT COUNT(*) FROM ChatMessages as c WHERE c.MemberID=m.MemberID)*.1 )
     as PointsEarned
   FROM Members as m

Can anybody tell me how I should do it with a single query?

Thanks!

© Stack Overflow or respective owner

Related posts about mysql