将两个表中的数据放入一个视图中
是否可以将两个表(具有相同字段)的数据抓取到一个视图中。基本上,视图将数据视为一张表。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,使用 UNION -
...要求列数相同,并且每个位置的数据类型都匹配。
..最好使用 JOIN:
但我想警告不要依赖视图 - 如果没有具体化,它们只是准备好的 SQL 语句。没有任何性能优势,并且如果您基于另一个视图构建视图,可能会对性能产生负面影响。此外,视图很脆弱 - 它们可能会发生变化,并且在使用支持视图之前您不会知道是否存在问题。
Yes, using a UNION -
...requires that there be the same number of columns, and the data types match at each position.
..preferrably, using a JOIN:
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.
笔记:
视图中的列是在创建视图时设置的。创建视图后向 table_1 和 table_2 添加列不会显示在 view_name 中。您将需要重新运行上述 DDL 才能显示新列。
如果您希望将重复行折叠为单行(但可能会给服务器带来更多工作):
通常在选择列表中使用
*
是不好的形式,但假设使用视图的查询是要选择他们需要的内容,我会在这里使用它,而不是明确命名所有列。 (特别是因为我不想在 table_1 和 table_2 更改时添加列名。)Note:
The columns in the view are set at the time the view is created. Adding columns to table_1 and table_2 after view creation will not show up in view_name. You will need to re-run the above DDL to get new columns to show up.
If you want duplicate rows to be collasped to single rows (but potentially more work for the server):
Generally it is bad form to use
*
in the select list, but assuming that the queries using the view are going to choose just what they need, I would use it here instead of explicitily naming all the columns. (Especially since I wouldn't want to have to add the column names when table_1 and table_2 change.)