SQL 如何在sql中的单个视图中加入两个视图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33568147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 04:09:57  来源:igfitidea点击:

how to join two views in single View in sql?

sql

提问by Sarfaraz Ali

I have two Views I want to merge into single view for example

例如,我有两个视图要合并为单个视图

View 1 is defined as:

视图 1 定义为:

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

View 2 is defined as:

视图 2 定义为:

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'

How do I combine them into a single view select * from View1 Select * from View2

如何将它们组合成一个视图 select * from View1 Select * from View2

output into single query

输出到单个查询

please help

请帮忙

回答by CM2K

if you want the common fields you can do:

如果你想要常见的字段,你可以这样做:

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

If the table structure is the same for both and you don't care about duplicates you can do an union allif you care about the duplicates then union

如果两者的表结构相同并且您不关心重复项,则可以执行union allif 您关心重复项union

select * from View1
union all
select * from View2

回答by Alex Gidan

Use unionif you have homogeneous columns. Otherwise use Joinon a suitable field

union如果您有同质列,请使用。否则Join在合适的领域使用

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

More details are needed to know which join to use and on which fields.

需要更多详细信息才能知道要使用哪个联接以及在哪些字段上使用。