gpt4 book ai didi

sql - 避免子查询

转载 作者:行者123 更新时间:2023-12-04 14:12:54 25 4
gpt4 key购买 nike

同学们,我有一个查询如下:

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

或者我必须将我的子查询变成一个连接?

最佳答案

看看 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

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

关于sql - 避免子查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6387604/

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