mysql如何根据各自条件批量更新

查看:504
本文介绍了mysql如何根据各自条件批量更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

现有两张表,A是明细表

id      uid       cost
1.       233.      0
2.        244.     1
3.       133.      4
4.        133.      3

现在要把把该表中cost大于0的数据,根据uid分组然后更新到另一个表上

select uid,count(cost) as count from A where cost > 0 group by uid 

这是取出的语句,有没办法一句话把这个查询结果根据uid更新到另一张表上?

解决方案

update B b set b.col=(select count(a.cost) as count from A a where cost > 0 and a.uid=b.uid group by uid)

这篇关于mysql如何根据各自条件批量更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆