当我尝试将两个子查询连接在一起时,第一个查询中的值发生了变化。知道为什么吗?谢谢!
这是代码:
SELECT *
FROM ((
SELECT HOME, VISITOR, COUNT("result") AS HGOALS
FROM(
SELECT HOME, VISITOR, "result"
FROM ENGLAND
WHERE TIER = 1 AND "SEASON" >= 1980 AND "result" = 'H'
)
GROUP BY HOME, VISITOR
ORDER BY HGOALS DESC)
JOIN
(SELECT HOME, VISITOR, COUNT("result") AS AGOALS
FROM(
SELECT HOME, VISITOR, "result"
FROM ENGLAND
WHERE TIER = 1 AND "SEASON" > 1980 AND "result" = 'A'
)
GROUP BY HOME, VISITOR) USING (VISITOR, HOME))
ORDER BY AGOALS DESC;
输出的部分是
Manchester United Aston Villa 5 18
Arsenal West Ham United 5 17
Arsenal Aston Villa 6 17
Manchester United Everton 12 16
Liverpool Aston Villa 8 16
但是当我只执行 JOIN 的第一部分时,即
SELECT HOME, VISITOR, COUNT("result") AS HGOALS
FROM(
SELECT HOME, VISITOR, "result"
FROM ENGLAND
WHERE TIER = 1 AND "SEASON" >= 1980 AND "result" = 'H'
)
GROUP BY HOME, VISITOR
ORDER BY HGOALS DESC
结果的一部分是:
Manchester United Tottenham Hotspur 27
Arsenal Everton 26
Manchester United Aston Villa 26
Liverpool Tottenham Hotspur 25
Manchester United West Ham United 24
请注意,对于
Manchester United Aston Villa 5 18
Manchester United Aston Villa 26
我应该得到的结果是 26,但是当我将两个子查询连接在一起时它变成了 5。为什么?