SQL - WHERE 子句中的 With 语句

发布于 2024-11-02 01:27:14 字数 600 浏览 3 评论 0原文

您好,

WITH 状态可以存在于 WHERE 子句中吗?

例如:

SELECT tbl1.name , tbl1.ID  
FROM DBTABLE0001 AS tbl1
WHERE  ( 
    exists(

        WITH H (super, ID, depth) AS 
        ( 
        SELECT ROOT.parent, ROOT.ID , 0 
        FROM DBTABLE0001 ROOT  
        WHERE ROOT.ID = tbl1.ID 

        UNION ALL 

        SELECT PARENT.parent, PARENT.ID , CHILD.depth + 1 
        FROM H CHILD, DBTABLE0001 PARENT 
        WHERE PARENT.ID = CHILD.super 

        )

        SELECT ID 
        FROM H 
        WEHER H.ID = "abcd"
        ORDER BY depth

    ) 
)  

谢谢, 莫。

HI,

Does a WITH state can live in a WHERE clause?

for instance:

SELECT tbl1.name , tbl1.ID  
FROM DBTABLE0001 AS tbl1
WHERE  ( 
    exists(

        WITH H (super, ID, depth) AS 
        ( 
        SELECT ROOT.parent, ROOT.ID , 0 
        FROM DBTABLE0001 ROOT  
        WHERE ROOT.ID = tbl1.ID 

        UNION ALL 

        SELECT PARENT.parent, PARENT.ID , CHILD.depth + 1 
        FROM H CHILD, DBTABLE0001 PARENT 
        WHERE PARENT.ID = CHILD.super 

        )

        SELECT ID 
        FROM H 
        WEHER H.ID = "abcd"
        ORDER BY depth

    ) 
)  

Thanks,
Mo.

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

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

发布评论

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

评论(1

︶ ̄淡然 2024-11-09 01:27:14

您应该将 with 语句放在查询的开头。那么你可以在任何地方使用它:

WITH H (super, ID, depth) AS           
            (SELECT ROOT.parent, ROOT.ID , 0 
                  FROM DBTABLE0001 ROOT            
              WHERE ROOT.ID = DBTABLE0001.ID             
              UNION ALL             
              SELECT PARENT.parent, PARENT.ID , CHILD.depth + 1                         FROM H CHILD, DBTABLE0001 PARENT           
                WHERE PARENT.ID = CHILD.super) 

SELECT tbl1.name , tbl1.ID    
    FROM DBTABLE0001 AS tbl1  
    WHERE  (exists(SELECT ID FROM H WEHER H.ID = "abcd"  ORDER BY depth)) 

You should place your with statements at the begining of your query. then you can use it every where :

WITH H (super, ID, depth) AS           
            (SELECT ROOT.parent, ROOT.ID , 0 
                  FROM DBTABLE0001 ROOT            
              WHERE ROOT.ID = DBTABLE0001.ID             
              UNION ALL             
              SELECT PARENT.parent, PARENT.ID , CHILD.depth + 1                         FROM H CHILD, DBTABLE0001 PARENT           
                WHERE PARENT.ID = CHILD.super) 

SELECT tbl1.name , tbl1.ID    
    FROM DBTABLE0001 AS tbl1  
    WHERE  (exists(SELECT ID FROM H WEHER H.ID = "abcd"  ORDER BY depth)) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文