gpt4 book ai didi

SQL INNER JOINing 2 个子查询

转载 作者:行者123 更新时间:2023-12-03 18:42:17 25 4
gpt4 key购买 nike

我正在尝试将这两个子查询(我认为这就是所谓的)内部连接在一起,其中第一个查询的 branchName 等于第二个查询的 branchName。

但是,他们似乎不想联合起来,而且以我对 SQL 的有限了解,我似乎无法找到解决此问题的方法。我尝试在各种位置移动括号,但它也不喜欢那样。

SELECT * 
FROM
(
SELECT B.branchName, A.type, AVG (T.amount), COUNT(A.accNumber)
FROM Branch B, Account A, Transactions T
WHERE
B.branchNumber = A.branchNumber AND
A.accNumber = T.accNumber
GROUP BY B.branchName, A.type
)

INNER JOIN

(
SELECT B1.branchName, COUNT(A1.accNumber)
FROM Account A1, Branch B1
WHERE
A1.branchNumber = B1.branchNumber
GROUP BY B1.branchName
HAVING COUNT(A1.accNumber) > 5
)
ON
B.branchName = B1.branchName

最佳答案

为了使查询工作,您需要命名内部选择(子查询):

SELECT * 
FROM
(
SELECT B.branchName, A.type, AVG (T.amount), COUNT(A.accNumber)
FROM Branch B, Account A, Transactions T
WHERE
B.branchNumber = A.branchNumber AND
A.accNumber = T.accNumber
GROUP BY B.branchName, A.type
) q1

INNER JOIN

(
SELECT B1.branchName, COUNT(A1.accNumber)
FROM Account A1, Branch B1
WHERE
A1.branchNumber = B1.branchNumber
GROUP BY B1.branchName
HAVING COUNT(A1.accNumber) > 5
) q2
ON
q1.branchName = q2.branchName

关于SQL INNER JOINing 2 个子查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33603744/

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