【发布时间】:2019-08-10 11:30:54
【问题描述】:
从 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
Table2 优先于 Table1。当 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
【问题讨论】:
-
包含两个表的视图假定某种连接。如果这些表以某种方式相关,那是有道理的。我看不到这两个表之间有任何关系,所以我不知道如何创建一个视图以便它返回所需的数据。此外,ID = 01 是从哪里来的?它在您的示例表中不存在。
-
@Littlefoot,任何其他方法也可以(而不是视图有什么方法可以创建它)