从2个表创建视图,其中一个表优先于其他表(大多数情况下,两个表相似,除了一个列)。
需要2个表的视图。
Table1 table contains below columns with values:
ValueOne = C11
ValueTwo= C12
ValueThree= C13
Table2 table contains below columns (extra id column compare to table1).
Id = 123
ValueOne = C11
ValueTwo= V12
ValueThree= C13
表2优先于表1。当w用ID查询时,如果Id不存在,则必须从Table1中选择值。如果Id可用,则必须从Table2中选择值。
为此,我需要一个视图来合并这两个表,并且当我们从视图中查询时,我们需要获取正确的结果。
example: 1) Select * from ViewName where ID=123
then in this case I have to get below values (from table2, as the ID exist in the table2):
Id = 123
ValueOne = C11
ValueTwo= V12
ValueThree= C13
2) Select * from ViewName where ID=01
in this case it has to get the below values (Id and other values from Table1,
as ID is not there in Table2:
Id = 01
ValueOne = C11
ValueTwo= C12
ValueThree= C13
答案 0 :(得分:2)
您将需要使用联接。这样的事情应该起作用
CREATE VIEW View_1 AS
select t2.*
from Table2 t2
Union ALL
Select t1.*
from table1 t1
LEFT JOIN Table2 t2 on t1.ID = t2.ID
where t2.ID is NULL
我没有测试,但这应该给您一个良好的开端。