sql语句怎么求两个表值相同但金额不同

a表
名称 价格
a1 a2

b表
名称 价格
b1 b1
b1 b2

a.a1 =b.b1
a.a2=b.b1+b2

想求出在a.a1 =b.b1 的情况下, a.a2!=b.b1+b2

第1个回答  2014-01-06
select 名称 from 
(
select 名称,sum(价格) as 总价 from a group by 名称
union 
select 名称,sum(价格) as 总价 from b group by 名称
)  t1 group by 名称 having count(名称) > 1

首先union 会过滤重复的,加入a和b里面的 价格之和 相同,且 名称相同 就只会剩余一条显示,再用group by 分组,求得名称不止一条的,就代表不相同

第2个回答  2014-01-06
select a.名称,a.价格
from a ,b
where a.名称 =b.名称
group by a.名称,a.价格
having a.价格!=sum(b.价格)
第3个回答  2014-01-06
select * from a where a.价格<>(select sum(b.价格) from b where b.名称=a.名称)
这样可以么?
第4个回答  2014-01-06
你确定是【a.a2!=b.b1+b2】?

追问

b表的数据有点乱,我改下b表 名称 价格 b1 b3 b1 b2

a.a2!=b.b3+b2

追答

select * from a1, (select b.mingcheng, sum(b.jiage) jiage from b2 b group by jiage) b where a.mingcheng = b.mingcheng and a.jiage != b.jiage;

---
先计算出b表中的数据,然后在和A表进行关联。

本回答被提问者采纳
相似回答
大家正在搜