mysql sum去除重复

查看:179
本文介绍了mysql sum去除重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

A表

id    name   price
1      a       1
2      b       1

B表

id      type
1        hh
1        hh
2        cc


select sum(price) from A as a left join B as b on a.id=b.id where b.type=hh


返回结果2,现在我想根据A表id(distinct)统计,得出sum(price)为1的sql语句怎么写?

解决方案

B表两条id为1的记录,left join查询会得到两条记录并求和,所以就为2了,你在B表中删除一条id为1的数据,应该就对了。
但是问题并没有解决,可以这样:

select sum(price) from A as a left join (select id,type from B group by id) as temp on a.id=temp.id where temp.type=hh

由于没有任何开发环境,没法测试,大致就这样吧。

这篇关于mysql sum去除重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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