欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

问下这个SQL的写法

程序员文章站 2024-02-17 21:45:28
...
本帖最后由 u011669689 于 2013-09-25 18:25:00 编辑

sql select update

基础条件:
user表 userid ,username
user_money 表 u_id,user_money
user_money_exchange 表:money ,username,ordertag =1或2

现在想执行:

update user_money set user_money = (user_money_exchange 当ordertag =1的 sum(money) + 当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


这个是错的,想知道对的是?谢谢各位了

回复讨论(解决方案)

先获取相关的数据出来,之后在执行update。印象中不能这么写

试试

update user_money um left join user u on um.u_id=u.useridleft join  (select username,sum(money) as moneys from user_money_exchange group by username) umeon ume.username=u.usernameset um.money = ume.moneys

谢谢,可行,非常感谢