日期:2014-05-18  浏览次数:20540 次

求助,三表联合查询的SQL语句怎么写?
现存在以下三张表

    表room  
房号 客户ID 购房日期 总价
A001 00001 2007-02-03 154000
A002 00002 2007-02-28 160000
A003 00003 2007-03-15 180000
表   custom
客户ID 客户名称 电话
00001 张三 020-12345678
00002 李四 020-32145678
00003 王五 020-54321878
表   getin
房号 收款名称 收款日期 收款金额
A001 首付 2007-02-03 50000
A001 房款 2007-05-01 100000
A002 首付 2007-02-28 60000
A003 房款 2007-05-30 100000

现需查询出未交清款项的房号,客户名称,已付款,未付款,这条语句该怎么写呀?


------解决方案--------------------
select room.房号,custom.客户名称,getin1.已付款,(room.总价-getin1.已付款) AS 未付款
from room
left join (select getin.房号,sum(getin.收款金额) AS 已付款 from getin group by getin.房号) getin1
ON getin1.房号=room.房号
left join custom ON custom.客户ID=room.客户ID
楼主看看如此表达是否符合...
------解决方案--------------------
---lz试试这个
Select
A.roomid As 房间ID,
A.customID As 客户ID,
B.customName As 客户姓名,
IsNull((Select Sum(g_money) From getin Where roomID=A.roomID),0) As 已付款,
A.sumPrice-IsNull((Select Sum(g_money) From getin Where roomID=A.roomID),0) As 未付款
From
room A
Left Join
custom B
On A.customID=B.customID