0

TContractFormフィールドのあるテーブルがありますidcontract(primary key),idcntrtype(foreign key),date_begin,date_end,cost。また、私は3種類の契約を結んでいます。

TRentWhouseContractフィールド付きidcontract,idclient,idpdtwhs

TRentShoppointContractフィールド付きidcontract,idclient,idshoppoint,idshoptype

TRentEquipContractフィールド付きidcontract,idclient,ideq,amount

idcntrtype- 契約の種類。TContractType- idcntrtype(primary key),idcntrclass(foreign key),nameTContractClass- idcntrclass,name

列のあるビューを作成したい idcontract,idclient,contract_type(name of type),contract_class(name of class),date_begin,date_end,cost

しかし、私のクエリは何も返しません(TRentWhouseContractテーブルにいくつかのコントラクトがあることを知っています(他のテーブルには行がありません)(いくつかのテーブルが空の場合に別のテーブルから同じidclientを取得する方法がわからないため、idclientがありません) 。

select TCF.idcontract,
   --idclient
   TCF.date_begin,
   TCF.date_end,
   TCT.name as [type],
   TCC.name as class,
   TCF.cost
from TContractForm as TCF,
 TRentEquipContract as TREC,
 TRentShopPointContract as TRSPC,
 TRentWhouseContract as TRWC,
 TContractType as TCT,
 TContractClass as TCC
where TCF.idcontract = TREC.idcontract or TCF.idcontract = TRSPC.idcontract
  or TCF.idcontract = TRWC.idcontract and TCT.idcntrtype = TCF.idcntrtype
  and TCT.idcntrclass = TCC.idcntrclass
4

2 に答える 2

2

私が正しく理解していれば、これは私がすることです

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
于 2012-12-14T18:29:25.963 に答える
1

idclient の取得方法:

coalesce(TREC.idclient, TRSPC.idclient,TRWC.idclient) as idclient

合体は最初の非欠損値を返します。

残りについては、カンマ結合ではなく左外部結合を行うべきだと思います。すべてのテーブルに一部のレコードがないためです (まあ、すべてのテーブルにレコードはありません!)。

select...
from TCF,TCT,TCC ... A
 left join ... TREC on A.idcontract=TREC.idcontract
 left join ... TRSPC 

于 2012-12-14T18:28:06.083 に答える