gpt4 book ai didi

SQL - WHERE 子句中的 With 语句

转载 作者:行者123 更新时间:2023-12-04 18:31:04 24 4
gpt4 key购买 nike

你好,

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

)
)

谢谢,莫.

最佳答案

您应该将 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))

关于SQL - WHERE 子句中的 With 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5692565/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com