2015-11-06 48 views
-2

我有兩個意見我要合併到單一視圖,例如如何在單個View中加入兩個視圖在sql中?

查看1被定義爲:

CREATE VIEW view2 
AS 
select DV.valueId, DV.value,E.ElementId,E.name, 
     ES.ElementSetGroupId,ET.entryId,ET.name as E_Name, 
     ET.SystemDateTime,C.compositionId,C.linkSetRef 
from DataValue DV 
inner join Element E 
     on DV.valueId=E.valueRef 
inner join ElementSet ES 
     on E.ElementId=ES.ElementRef 
inner join Entry ET 
     on ES.elementSetGroupId=ET.elementSetGroupRef 
inner join EntrySet ESG 
     on ESG.entryRef=ET.entryId 
inner Join Section S 
     on s.entrySetGroupRef=ESG.entrySetGroupId 
inner join SectionSet SS 
     on SS.sectionRef=S.sectionId 
where S.name='Appointment' 

如何將它們結合起來:

CREATE VIEW view1 AS 
select DV.demValueId,DV.value,DE.demElementId,DE.name, 
     DES.demElementSetGroupId,DR.demRecordId,DR.name as Role, 
     DR.demClusterSetGroupRef,L.EmailId 
from DemographicsDataValue DV 
inner join DemographicsElement DE 
     on DV.demValueId=DE.demValueRef 
inner join DemographicsElementSet DES 
     on DE.demElementId=DES.demElementRef 
inner join DemographicsRecord DR 
    on DR.demElementSetGroupRef=DES.demElementSetGroupId 
inner join LoginDetail L 
    on DR.demRecordId=L.demRecordIdRef 

查看2被定義成單一視圖 select * from View1 選擇* from View2

輸出成單查詢

請幫忙

+1

列在每個視圖中是否相同?假設是這樣,使用'union' ... – sgeddes

+0

沒有兩個視圖有不同的列 –

+0

將它們視爲表並將它們連接到適當的字段。 –

回答

0

如果您有同質性列,請使用union。否則,在適當的領域,需要

SELECT * 
FROM view1 
INNER JOIN view2 ON view1.id = view2.ref; 

更多細節知道哪些加入使用,並在其領域的應用Join

+0

我有不同的列名稱與不同的值在兩個視圖中,他們不匹配each.then我做 –

+0

@SarfarazAli請顯示您的視圖的列和數據類型和您的預期輸出,否則我們只是猜測! –

+0

In view1查詢爲「從DemographicsDataValue DV內部加入DemographicsElement中選擇DV.demValueId,DV.value,DE.demElementId,DE.name,DES.demElementSetGroupId,DR.demRecordId,DR.name as Role,DR.demClusterSetGroupRef,L.EmailId DE on DV.demValueId = DE.demValueRef內部連接DemographicsElementSet DES上的DE.demElementId = DES.demElementRef內部連接DemographicsRecord DR on DR.demElementSetGroupRef = DES.demElementSetGroupId內部連接LoginDetail L上DR.demRecordId = L.demRecordIdRef「 –

0

,如果你想在公共字段,你可以這樣做:

select * FROM View1 
inner join View2 -- or you can do a left join here 
on View1.Column = View2.Column -- and ... add more if needed 

如果表結構上是相同的,你不關心重複,如果你關心的重複,那麼你可以做一個union allunion

select * from View1 
union all 
select * from View2 
+0

@ CM @ K:在我的兩個視圖中都有不同的列名,其值不同 –

相關問題