问下这个SQL的写法

php中文网
发布: 2016-06-23 14:22:38
原创
1037人浏览过

本帖最后由 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
登录后复制

谢谢,可行,非常感谢

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号