Sql 查询的麻烦

Sql 查询的麻烦,sql,Sql,我有一个表TContractForm,表中有idcontract(主键)、idcntrtype(外键)、date\u begin、date\u end、cost字段。我还有三种合同: TrenthouseContract带有字段idcontract、idclient、idpdtwhs TRentShoppointContract带有字段idcontract、idclient、idshoppoint、idshoptype TRentEquipContract带字段idcontract、idclie

我有一个表
TContractForm
,表中有
idcontract(主键)、idcntrtype(外键)、date\u begin、date\u end、cost
字段。我还有三种合同:

TrenthouseContract
带有字段
idcontract、idclient、idpdtwhs

TRentShoppointContract
带有字段
idcontract、idclient、idshoppoint、idshoptype

TRentEquipContract
带字段
idcontract、idclient、ideq、amount

idcntrtype
-合同类型
t合同类型
-
idcntrtype(主键)、idcntrclass(外键)、name
<代码>合同类-
idcntrclass,名称

我想创建一个包含列的视图
idcontract、idclient、合同类型(类型名称)、合同类别(类别名称)、开始日期、结束日期、成本

但我的查询没有返回任何内容(我知道TRentWhouseContract表中有几个契约(其他表没有行)(它没有idclient,因为如果某个表为空,我不知道如何从不同的表中获取相同的idclient)

如何获取idclient:

coalesce(TREC.idclient, TRSPC.idclient,TRWC.idclient) as idclient
Coalesce返回第一个非缺失值

至于其余的-我认为您应该做左外部连接而不是逗号连接,因为您没有在所有表中都有一些记录(嗯,所有表中都没有记录!)

等等。

如何获取idclient:

coalesce(TREC.idclient, TRSPC.idclient,TRWC.idclient) as idclient
Coalesce返回第一个非缺失值

至于其余的-我认为您应该做左外部连接而不是逗号连接,因为您没有在所有表中都有一些记录(嗯,所有表中都没有记录!)


等等。

如果我理解正确,我会这样做

select TCF.idcontract,
   --idclient
   TCF.date_begin,
   TCF.date_end,
   TCT.name as [type],
   TCC.name as class,
   TCF.cost
from TContractForm as TCF
JOIN TContractType as TCT
   ON TCT.idcntrtype=TCF.idcntrtype
JOIN TContractClass as TCC
   ON TCT.idcntrclass = TCC.idcntrclass
UNION
select TCF.idcontract,
   --idclient
   TCF.date_begin,
   TCF.date_end,
   TCT.name as [type],
   TCC.name as class,
   TCF.cost
from TRentWhouseContract as TCF
JOIN TContractType as TCT
   ON TCT.idcntrtype=TCF.idcntrtype
JOIN TContractClass as TCC
   ON TCT.idcntrclass = TCC.idcntrclass
UNION
select TCF.idcontract,
   --idclient
   TCF.date_begin,
   TCF.date_end,
   TCT.name as [type],
   TCC.name as class,
   TCF.cost
from TRentShoppointContract as TCF
JOIN TContractType as TCT
   ON TCT.idcntrtype=TCF.idcntrtype
JOIN TContractClass as TCC
   ON TCT.idcntrclass = TCC.idcntrclass
UNION
select TCF.idcontract,
   --idclient
   TCF.date_begin,
   TCF.date_end,
   TCT.name as [type],
   TCC.name as class,
   TCF.cost
from TRentEquipContract as TCF
JOIN TContractType as TCT
   ON TCT.idcntrtype=TCF.idcntrtype
JOIN TContractClass as TCC
   ON TCT.idcntrclass = TCC.idcntrclass

如果我理解正确,我会这么做

select TCF.idcontract,
   --idclient
   TCF.date_begin,
   TCF.date_end,
   TCT.name as [type],
   TCC.name as class,
   TCF.cost
from TContractForm as TCF
JOIN TContractType as TCT
   ON TCT.idcntrtype=TCF.idcntrtype
JOIN TContractClass as TCC
   ON TCT.idcntrclass = TCC.idcntrclass
UNION
select TCF.idcontract,
   --idclient
   TCF.date_begin,
   TCF.date_end,
   TCT.name as [type],
   TCC.name as class,
   TCF.cost
from TRentWhouseContract as TCF
JOIN TContractType as TCT
   ON TCT.idcntrtype=TCF.idcntrtype
JOIN TContractClass as TCC
   ON TCT.idcntrclass = TCC.idcntrclass
UNION
select TCF.idcontract,
   --idclient
   TCF.date_begin,
   TCF.date_end,
   TCT.name as [type],
   TCC.name as class,
   TCF.cost
from TRentShoppointContract as TCF
JOIN TContractType as TCT
   ON TCT.idcntrtype=TCF.idcntrtype
JOIN TContractClass as TCC
   ON TCT.idcntrclass = TCC.idcntrclass
UNION
select TCF.idcontract,
   --idclient
   TCF.date_begin,
   TCF.date_end,
   TCT.name as [type],
   TCC.name as class,
   TCF.cost
from TRentEquipContract as TCF
JOIN TContractType as TCT
   ON TCT.idcntrtype=TCF.idcntrtype
JOIN TContractClass as TCC
   ON TCT.idcntrclass = TCC.idcntrclass

我应该使用
合并
内部
选择
?我应该使用
合并
内部
选择