将两个表中的数据合并到一个视图

将两个表中的数据合并到一个视图

本文介绍了将两个表中的数据合并到一个视图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将两个表(具有相同的字段)中的数据捕获到一个视图中.基本上,因此视图将数据视为一个表.

Is it possible to grab data from two tables (that have the same fields) into one view. Basically, so the view sees the data as if it was one table.

推荐答案

是的,使用UNION-

Yes, using a UNION -

CREATE VIEW vw_combined AS
   SELECT * FROM TABLE1
   UNION ALL
   SELECT * FROM TABLE2

...要求列数相同,并且每个位置的数据类型都匹配.

...requires that there be the same number of columns, and the data types match at each position.

..最好使用JOIN:

..preferrably, using a JOIN:

CREATE VIEW vw_combined AS
   SELECT *
    FROM TABLE1 t1
    JOIN TABLE2 t2 ON t2.col = t1.col

但是我要警告不要依赖于视图-如果未实现,它们只是准备好的SQL语句.没有性能优势,如果您基于另一个视图构建视图,则会对性能产生负面影响.另外,视图很脆弱-它们可能会发生变化,如果有问题,您只有在使用辅助视图时才能知道.

But I want to warn against depending on views - if not materialized, they are only prepared SQL statements. There's no performance benefit, and can negatively impact performance if you build a view based on another. Also, views are brittle - they can change, and you won't know until using a supporting view if there are issues.

这篇关于将两个表中的数据合并到一个视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 18:45