超难度SQL语句(30分)

  • 主题发起人 主题发起人 devexpress
  • 开始时间 开始时间
D

devexpress

Unregistered / Unconfirmed
GUEST, unregistred user!
主表table1
编号(bh) 姓名(name) 金额(money)
1 张三 30
2    李四 40
3 张三 100
4 张三  60
5 王五 500

从表table2
编号(bh) 单价(dj) 数量(sl)
1 2 10
1 2 5
3 4 20
4 15 2
4 15 2

例子数据如上。

要求结果

table1表显示如下
编号(bh) 姓名(name) 金额(money)
1 张三 30
4 张三  60

条件是(table2)从表的sum(dj*sl)=table1.money 成立
按bh进行了分组
这条语句怎样写.

谢谢
 
试试
Select 主表.bh,主表.name,主表.money
From 主表 Inner Join 从表 On 主表.bh=从表.bh
Where money In (Select Sum(dj*sl) From 从表 Group By bh)
 
谢谢你,可以实现,

select tabel1.bh,tabel1.name,tabel1.money from tabel1 A,
(select bh,sum(dj*sl) as money from table2 group by bh) B
where (A.bh = B.bh) and (A.money= B.money)

这样也可以. 但我想你的写法更好。
 
加上
Order By 主表.bh
这样结果就更清楚了。
 
后退
顶部