SQL-使用CTE对另一个CTE的引用 [英] SQL - Use a reference of a CTE to another CTE

查看:72
本文介绍了SQL-使用CTE对另一个CTE的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在SQL中是否可以在同一查询的另一个C.T.E中的Common Table Expression中使用引用?这里有一个示例:

Is it possible in SQL use a reference inside a Common Table Expression inside another C.T.E in the same query? Here there is an example:

WITH CT1 AS (SELECT * FROM T),
     CT2 AS (SELECT * FROM CT1)

SELECT * FROM CT2;

我在SQLite3中尝试过此方法,但它确实有效,我只是想知道它是否属于标准SQL。关于此论点的任何建议将不胜感激。

I tried this in SQLite3 and it works, I just wanted to know if it's part of standard SQL. Any advices concerning this argument will be highly appreciated. Thank you very much!

推荐答案

以下是CTE的三个重要属性:

Here are three important properties of CTEs:


  • 您可以在后续CTE或查询主体中引用CTE。

  • You can refer to a CTE in subsequent CTEs or in the main body of the query.

您可以多次引用任何给定的CTE。

You can refer to any given CTE multiple times.

CTE可以在 from 子句中使用其他子查询中的任何嵌套级别。

The CTE can be used in a from clause at any level of nesting within other subqueries.

CTE(与SQL中的所有内容一样)需要定义在使用它们之前。因此,您不能以随机顺序定义它们。

The CTEs -- as with everything in SQL -- need to be defined before they are used. So, you cannot define them in random order.

这是CTE的标准定义,并且很好地解释了如何在数据库中使用它们。这三个属性是它们与子查询不同的关键方式。

This is the standard definition of CTEs and does a good job of explaining how they are used across databases. Those three properties are key ways that they differ from subqueries.

这篇关于SQL-使用CTE对另一个CTE的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆