从 SQL 脚本引用其他 SQL 脚本?

发布于 2024-10-26 20:39:57 字数 213 浏览 0 评论 0原文

我目前正在将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ゞ花落谁相伴 2024-11-02 20:39:57

您可能正在寻找视图。

视图基本上是 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.

荒人说梦 2024-11-02 20:39:57

您可以创建一个 VIEW 作为查询,然后在另一个查询中引用该视图。

CREATE VIEW <viewname> AS <SELECT STATEMENT>

然后

SELECT * FROM <viewname> INNER JOIN <other table>

You can create a VIEW as a query, then reference the view in another query.

CREATE VIEW <viewname> AS <SELECT STATEMENT>

then

SELECT * FROM <viewname> INNER JOIN <other table>
放我走吧 2024-11-02 20:39:57

是的。它们被称为视图。

您可以创建一个视图

CREATE VIEW vw_some_query AS
SELECT * FROM
 table_a LEFT INNER JOIN table_b ON table_a.id = table_b.id2

,然后您可以编写如下选择:

SELECT * FROM vw_some_query LEFT INNER JOIN table_c ON vw_some_query.id = table_c.id3

Yes. They are called views.

You can create a view like

CREATE VIEW vw_some_query AS
SELECT * FROM
 table_a LEFT INNER JOIN table_b ON table_a.id = table_b.id2

then you can write a select like:

SELECT * FROM vw_some_query LEFT INNER JOIN table_c ON vw_some_query.id = table_c.id3
乞讨 2024-11-02 20:39:57

SQL中有类似的东西吗?

是的。在SQL中你可能会使用WITH子句:

WITH someData AS
(
  select a.col1, b.col2
  from tableA a join tableB b
  on (a.someKey = b.someKey)
),
...
select data1.col1, data1.col2, data2.col3
from someData data1 join tableC data2
on (data1.col1 = data2.anotherKey)
where ...

视图也可以,但是要跟踪另一个数据库对象,如果使用物化视图,需要担心刷新快照表等。我的建议是与大量一起使用WITH尽可能的评论。

编辑:如果您发现自己一遍又一遍地询问数据库的相同问题,那么视图(或垫视图)会更合适。但除此之外,请在查询中保留逻辑。

Is there something like this in SQL?

Yes. In SQL you would probably use the WITH clause:

WITH someData AS
(
  select a.col1, b.col2
  from tableA a join tableB b
  on (a.someKey = b.someKey)
),
...
select data1.col1, data1.col2, data2.col3
from someData data1 join tableC data2
on (data1.col1 = data2.anotherKey)
where ...

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文