关于两个表sql语句的问题

2019-01-02 21:45发布

问题:

表1:Projects
字段:ID,ContractMoney(合同金额)

表2:Finances
字段Porject_ID,ReceivedMoney(回款金额)

(一对多的关系)

要求:筛选出 没有全部回款的项目 和
筛选出 全部回款的项目

一共两个sql语句

回答1:

  1. select * from Projects p left join (select Porject_ID sum(ReceivedMoney)ReceivedMoney from Finances group by Porject_ID) f on p.ID=f.Porject_ID
    where ContractMoney>isnull(ReceivedMoney ,0)

2.select * from Projects p left join (select Porject_ID sum(ReceivedMoney)ReceivedMoney from Finances group by Porject_ID) f on p.ID=f.Porject_ID
where ContractMoney<=isnull(ReceivedMoney ,0)



回答2:

百度搜索一下子查询,可以解决



回答3:

2.select * from Projects p left join (select Porject_ID sum(ReceivedMoney)ReceivedMoney from Finances group by Porject_ID) f on p.ID=f.Porject_ID
where ContractMoney<=isnull(ReceivedMoney ,0)



标签: