我正在使用拉曼Sabermetrics数据库来学习MySQL
我正在尝试查看哪些投手在所有数据中的本垒打总数最高。打击表包含“本垒打”(HR)列,而字段表包含“位置”列(POS)
我尝试了以下
SELECT playerID, SUM(HR) AS HR_SUM_TOTAL
FROM batting
GROUP BY playerID
WHERE (SELECT POS
FROM fielding
WHERE POS = "P")
ORDER BY HR_SUM_TOTAL DESC
有什么办法可以通过嵌套选择来做到这一点,还是将POS列添加到打击表中然后运行查询会更容易?
提前致谢
最佳答案
如果我正确理解您的问题,一种解决方法是
SELECT b.playerID, SUM(b.HR) HR_SUM_TOTAL
FROM batting b JOIN
(
SELECT DISTINCT playerID
FROM fielding
WHERE POS = 'P'
) f ON b.playerID = f.playerID
GROUP BY b.playerID
ORDER BY HR_SUM_TOTAL DESC
要么
SELECT playerID, SUM(HR) HR_SUM_TOTAL
FROM batting
WHERE playerID IN
(
SELECT DISTINCT playerID
FROM fielding
WHERE POS = 'P'
)
GROUP BY playerID
ORDER BY HR_SUM_TOTAL DESC
关于mysql - 使用不同表中的嵌套选择形成条件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40206482/