从 SQL 脚本引用其他 SQL 脚本?
我目前正在将 MS Access 查询转换为 SQL 查询,并注意到在 Access 查询中它似乎将另一个查询连接到其他表。所以我环顾四周,似乎该查询几乎使查询看起来更干净,而无需在同一脚本中包含各种子查询,
例如
FROM [query name] INNER JOIN [some other table]
SQL中有类似的东西吗?
I'm currently converting MS access queries to SQL queries and noticed that in the access query it appears to be joining another query to other tables. So I looked around and it seems like that query pretty much makes the query look cleaner without needing to have all sorts of subqueries in the same script
Something like
FROM [query name] INNER JOIN [some other table]
Is there something like this in SQL?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可能正在寻找视图。
视图基本上是
SELECT
查询的存储版本。它允许您引用结果集,而无需每次都重写查询。You are probably looking for VIEWS.
A view is basically a stored version of a
SELECT
query. It allows you to reference the result set without rewriting the query every time.您可以创建一个
VIEW
作为查询,然后在另一个查询中引用该视图。然后
You can create a
VIEW
as a query, then reference the view in another query.then
是的。它们被称为视图。
您可以创建一个视图
,然后您可以编写如下选择:
Yes. They are called views.
You can create a view like
then you can write a select like:
是的。在SQL中你可能会使用WITH子句:
视图也可以,但是要跟踪另一个数据库对象,如果使用物化视图,需要担心刷新快照表等。我的建议是与大量一起使用WITH尽可能的评论。
编辑:如果您发现自己一遍又一遍地询问数据库的相同问题,那么视图(或垫视图)会更合适。但除此之外,请在查询中保留逻辑。
Yes. In SQL you would probably use the WITH clause:
Views are ok too, but another db object to keep track of, and if using a materialized view, need to worry about refreshing snapshot table, etc. My suggestion is to use WITH along with plenty of comments where possible.
EDIT: If you find yourself asking the same question of the db over and over, then a view (or mat view) would be more appropriate. But otherwise, keep logic in the query.