我有以下“mlb_pitchers”表:
+------+-----------+-----------+-----+------+
| year | playerID | nameLast | IP | WAR |
+------+-----------+-----------+-----+------+
| 1903 | mathech01 | Mathewson | 366 | 9.6 |
| 1903 | youngcy01 | Young | 341 | 10.3 |
| 1904 | mathech01 | Mathewson | 367 | 8.4 |
| 1904 | walshed01 | Walsh | 110 | 1.1 |
| 1904 | youngcy01 | Young | 380 | 8.8 |
| 1905 | mathech01 | Mathewson | 338 | 14.9 |
| 1905 | walshed01 | Walsh | 136 | 3.0 |
| 1905 | youngcy01 | Young | 320 | 9.9 |
| 1906 | mathech01 | Mathewson | 266 | 1.5 |
| 1906 | walshed01 | Walsh | 278 | 7.2 |
| 1906 | youngcy01 | Young | 287 | 0.6 |
+------+-----------+-----------+-----+------+
这样做时:
SELECT playerID, nameLast, SUM(IP), SUM(WAR)
FROM mlb_pitchers
WHERE year >= '1903' AND year <= '1906'
GROUP BY playerID, nameLast
我得到了这个输出:
+-----------+-----------+---------+----------+
| playerID | nameLast | SUM(IP) | SUM(WAR) |
+-----------+-----------+---------+----------+
| mathech01 | Mathewson | 1337 | 34.4 |
| walshed01 | Walsh | 524 | 11.3 |
| youngcy01 | Young | 1328 | 29.6 |
+-----------+-----------+---------+----------+
如果投手在任何一年(1903-1906)投球,这种方法很有效。但我想排除一个在第一个搜索年(1903年)没有记录的投手。所以输出看起来像这样(排除沃尔什):
+-----------+-----------+---------+----------+
| playerID | nameLast | SUM(IP) | SUM(WAR) |
+-----------+-----------+---------+----------+
| mathech01 | Mathewson | 1337 | 34.4 |
| youngcy01 | Young | 1328 | 29.6 |
+-----------+-----------+---------+----------+
我希望只包括那些拥有1903年纪录的投手。或者相反 - 排除那些没有1903记录的人。 我无法弄清楚要添加到SELECT查询中的内容以实现此目的。这甚至可能吗?
答案 0 :(得分:2)
据推测,“year”是一个整数,所以不要使用引号。
如果您希望玩家的记录中包含1903,则可以使用having
:
SELECT playerID, nameLast, SUM(IP), SUM(WAR)
FROM mlb_pitchers
WHERE year >= 1903 AND year <= 1906
GROUP BY playerID, nameLast
HAVING SUM(CASE WHEN year = 1903 THEN 1 ELSE 0 END) > 0;
答案 1 :(得分:0)
如果你想只包括一段时间,你可以添加到where子句,但如果我理解你不想过滤几年但玩家。
SELECT playerID, nameLast, SUM(IP), SUM(WAR)
FROM mlb_pitchers
WHERE playerID In (Select PlayerId from mlb_pitchers where year = '1903')
GROUP BY playerID, nameLast
这给出了所有年份的统计数据,但仅包括拥有1903年记录的球员。