避免子查询

发布于 2024-11-16 02:24:12 字数 270 浏览 4 评论 0原文

各位同仁, 我有一个查询如下:

SELECT A.ID, B.ID, (HUGE SUBQUERY) as HS
FROM TABLE_A JOIN TABLE_B ON A.ID = B.ID
WHERE (HUGE SUBQUERY) > 0

我想避免重复子查询。 有没有什么方法可以将我的 WHERE 重写为类似

WHERE HS > 0

Or 我必须将子查询转换为联接?

Fellows,
I have a query as follows:

SELECT A.ID, B.ID, (HUGE SUBQUERY) as HS
FROM TABLE_A JOIN TABLE_B ON A.ID = B.ID
WHERE (HUGE SUBQUERY) > 0

I'd like to avoid repeating the subquery.
Is there any way to rewrite my WHERE as something like

WHERE HS > 0

Or I must turn my subquery into a join?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

汹涌人海 2024-11-23 02:24:12

查看 With 子句:

WITH HS AS (Huge subquery)

SELECT A.ID, B.ID, HS
FROM TABLE_A JOIN TABLE_B ON A.ID = B.ID
WHERE HS  > 0

OR

SELECT *
FROM
(
    SELECT A.ID, B.ID, (HUGE SUBQUERY) as HS
    FROM TABLE_A JOIN TABLE_B ON A.ID = B.ID
)
WHERE HS > 0

Look at With clause :

WITH HS AS (Huge subquery)

SELECT A.ID, B.ID, HS
FROM TABLE_A JOIN TABLE_B ON A.ID = B.ID
WHERE HS  > 0

OR

SELECT *
FROM
(
    SELECT A.ID, B.ID, (HUGE SUBQUERY) as HS
    FROM TABLE_A JOIN TABLE_B ON A.ID = B.ID
)
WHERE HS > 0
-柠檬树下少年和吉他 2024-11-23 02:24:12

您可以使用 CTE

WITH cteHS AS (
    SELECT xxx AS Value
        FROM Huge Subquery
)
SELECT A.ID, B.ID, cteHS.Value as HS
    FROM TABLE_A, cteHS
        JOIN TABLE_B ON A.ID = B.ID
    WHERE cteHS.Value > 0

You could use a CTE:

WITH cteHS AS (
    SELECT xxx AS Value
        FROM Huge Subquery
)
SELECT A.ID, B.ID, cteHS.Value as HS
    FROM TABLE_A, cteHS
        JOIN TABLE_B ON A.ID = B.ID
    WHERE cteHS.Value > 0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文