How can I handle this SQL statement?

Source: Internet
Author: User
Ask the writing method of this SQL statement. at last, the basic conditions for editing are: user table & nbsp; userid & nbsp ;, username & nbsp; user_money & nbsp; description of this SQL statement in table & nbsp
At the end of this post, the basic conditions are edited by u042569689 at 18:25:00:
Userid, username
User_money table u_id, user_money
User_money_exchange table: money, username, ordertag = 1 or 2

Now you want to execute:

Update user_money set user_money = (user_money_exchange when ordertag = 1 sum (money) + when ordertag = 2 sum (money) where user_money.u_id = user. userid



update user_money 
set user_money.money = 

(select sum(ume.money) as m , u.userid from user_money_exchange ume  left join user u on ume.username = u.username where ume.ordertag = 1 group by ume.username
 + 
select sum(ume.money) as m , u.userid from user_money_exchange ume  left join user u on ume.username = u.username where ume.ordertag = 2 group by ume.username) 

where user_money.u_id = u.userid



This is wrong. Is it true? Thank you for sharing SQL select update:
------ Solution --------------------
Try
update user_money um left join user u on um.u_id=u.userid
left join  
(select username,sum(money) as moneys from user_money_exchange group by username) ume
on ume.username=u.username
set um.money = ume.moneys

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.