That really depends on how you want this data to be displayed.
Joins are used for when you want to select data that is related and display it all in the same row, where Unions are used for appeneding data from two different select statements together.
In your case, a view should be used when you have a very complex query, and you want to simplify the select statement that is needed to select the data. It will make it less confusing if you are going to use the select statement over and over. There are plenty of other reasons to use views, but I do not think they are relavent to this question.
A left join B is a composite: ( A ∉ ( A ∩ B ) ) ∪ ( A ∩ B )
right join is the same as a left join. Just syntactic sugar to swap positions of the tables/sets involved. B right join A is identical to A left join B.
A full join B is also a composite: ( A ∉ ( A ∩ B ) ) ∪ ( A ∩ B ) ∪ ( B ∉ ( A ∩ B ) )
Which you use is entirely dependent on the problem.
A view is essentially an invariant, canned select statement. It lets you present "virtual tables", for instance, a de-normalized, flat view of a normalized database. Views are also useful for such things as restricting access to columns and/or tables.
发布评论
评论(2)
这实际上取决于您希望如何显示这些数据。
当您想要选择相关数据并将其全部显示在同一行中时,可以使用联接,而联合用于将两个不同选择语句中的数据附加在一起。
就您的情况而言,当您有非常复杂的查询并且您希望简化选择数据所需的 select 语句时,应使用视图。如果您要一遍又一遍地使用 select 语句,这会减少混乱。使用视图还有很多其他原因,但我认为它们与这个问题无关。
That really depends on how you want this data to be displayed.
Joins are used for when you want to select data that is related and display it all in the same row, where Unions are used for appeneding data from two different select statements together.
In your case, a view should be used when you have a very complex query, and you want to simplify the select statement that is needed to select the data. It will make it less confusing if you are going to use the select statement over and over. There are plenty of other reasons to use views, but I do not think they are relavent to this question.
@jworrin 说的话。
SQL 就是关于集合操作的。
A 内联接 B
设置交集:A ∩ BA union B
是...好吧...设置并集:A ∪ BA left join B
是一个组合:( A ∉ ( A ∩ B ) ) ∪ ( A ∩ B )右连接
与左连接相同。只是语法糖来交换所涉及的表/集的位置。B 右连接 A
与A 左连接 B
相同。全联接 B
也是复合: ( A ∉ ( A ∩ B ) ) ∪ ( A ∩ B ) ∪ ( B ∉ ( A ∩ B ) )使用哪个完全取决于问题。
view
本质上是一个不变的、固定的选择语句。它可以让您呈现“虚拟表”,例如,规范化数据库的非规范化平面视图。视图对于限制对列和/或表的访问也很有用。What @jworrin said.
SQL is all about set operations.
A inner join B
is set intersection: A ∩ BA union B
is ... well ... set union: A ∪ BA left join B
is a composite: ( A ∉ ( A ∩ B ) ) ∪ ( A ∩ B )right join
is the same as a left join. Just syntactic sugar to swap positions of the tables/sets involved.B right join A
is identical toA left join B
.A full join B
is also a composite: ( A ∉ ( A ∩ B ) ) ∪ ( A ∩ B ) ∪ ( B ∉ ( A ∩ B ) )Which you use is entirely dependent on the problem.
A
view
is essentially an invariant, canned select statement. It lets you present "virtual tables", for instance, a de-normalized, flat view of a normalized database. Views are also useful for such things as restricting access to columns and/or tables.