求助,三表联合查询的SQL语句怎么写?
现存在以下三张表
表room
房号客户ID购房日期总价
A001000012007-02-03154000
A002000022007-02-28160000
A003000032007-03-15180000
表 custom
客户ID客户名称电话
00001张三020-12345678
00002李四020-32145678
00003王五020-54321878
表 getin
房号收款名称收款日期收款金额
A001首付2007-02-0350000
A001房款2007-05-01100000
A002首付2007-02-2860000
A003房款2007-05-30100000
现需查询出未交清款项的房号,客户名称,已付款,未付款,这条语句该怎么写呀?
[解决办法]
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