SQL- 所有年份中出现的所有行
SQL- all rows which are present in all years
我的数据库中有以下列
Codes
year | code |
----------------
2009 | 10001 |
2009 | 10002 |
2009 | 10003 |
2010 | 10001 |
2010 | 10002 |
2010 | 10004 |
2011 | 10001 |
2011 | 10005 |
2011 | 10010 |
如何找到所有年份中预设的所有代码。例如在上面我只想要 10001
因为它是所有年份中唯一存在的代码。我试过了
SELECT code FROM Codes
GROUP BY code
HAVING count(year) = 3
但这不是很灵活,因为我可能想将其限制为一个子集,例如,仅查找 2010 年和 2011 年的所有代码。
SELECT code
FROM Codes
where year in (2010,2011,2012)
GROUP BY code
HAVING count( distinct year ) = 3
我的数据库中有以下列
Codes
year | code |
----------------
2009 | 10001 |
2009 | 10002 |
2009 | 10003 |
2010 | 10001 |
2010 | 10002 |
2010 | 10004 |
2011 | 10001 |
2011 | 10005 |
2011 | 10010 |
如何找到所有年份中预设的所有代码。例如在上面我只想要 10001
因为它是所有年份中唯一存在的代码。我试过了
SELECT code FROM Codes
GROUP BY code
HAVING count(year) = 3
但这不是很灵活,因为我可能想将其限制为一个子集,例如,仅查找 2010 年和 2011 年的所有代码。
SELECT code
FROM Codes
where year in (2010,2011,2012)
GROUP BY code
HAVING count( distinct year ) = 3