我想问一下,如果有多个条目有2个不同年份(2016年和2017年),我们如何查询Access数据库?我尝试使用'普通'查询WHERE / AND但它不准确 -
SELECT DNum
FROM tblNum
WHERE (DNum='0000' AND YEAR(DDate)=2016) OR YEAR(DDate)=2017)
示例DB:
[Value] [Date]
1234 1/1/16
1234 1/1/17
4321 1/1/16
4321 1/1/16
4321 2/2/16
1234将是'真实',因为它发生在2016年& 2017.任何帮助将不胜感激。感谢。
欢呼声,
答案 0 :(得分:1)
这在MS Access中可能有点棘手。这是一种有效两年的方法:
#include <sstream>
#include <string>
#include <boost/algorithm/string.hpp>
std::string line;
vector<string> strs;
std::ifstream infile("thefile.txt");
while (std::getline(infile, line))
{
strs.clear();
boost::split(strs,line,boost::is_any_of("\t"));
cout<<"age ="<<strs[1]<<endl;
}
如果您有任意年数,可以使用更复杂的SELECT DNum
FROM tblNum
WHERE YEAR(DDate) IN (2016, 2017)
GROUP BY DNum
HAVING MIN(YEAR(DDATE)) = 2016 AND MAX(YEAR(DDate)) = 2017;
条款执行此操作:
HAVING
请注意,此查询中并不一定要SELECT DNum
FROM tblNum
WHERE YEAR(DDate) IN (2015, 2016, 2017)
GROUP BY DNum
HAVING SUM(IIF(YEAR(DDATE) = 2015, 1, 0) > 0 AND
SUM(IIF(YEAR(DDATE) = 2016, 1, 0) > 0 AND
SUM(IIF(YEAR(DDATE) = 2017, 1, 0) > 0;
。它确实减少了聚合的数据量,这可以提高性能。
答案 1 :(得分:0)
您可以使用此查询:
SELECT a.DNum, a.DDate
FROM tblNum a INNER JOIN tblNum b
ON a.DNum = b.DNum AND
YEAR(a.DDate) <> YEAR(b.DDate)
您可以按以下过滤:
SELECT a.DNum, a.DDate
FROM tblNum a INNER JOIN tblNum b
ON a.DNum = b.DNum AND
YEAR(a.DDate) <> YEAR(b.DDate)
WHERE YEAR(a.DDate) in (2016,2017)
结果:
DNum DDate
1234 2016/01/01
1234 2017/01/17
如果您不需要DDate
使用DISTINCT
关键字来避免重复DNum:
SELECT DISTINCT a.DNum
FROM tblNum a INNER JOIN tblNum b
ON a.DNum = b.DNum AND
YEAR(a.DDate) <> YEAR(b.DDate)
WHERE YEAR(a.DDate) in (2016,2017)
结果:
DNum
1234