我在msaccess中有2个表,其中一个有多个重复记录,我想为同一个字段(日期)但不同的条件筛选它,我只想查看table2中存在的记录以及table1中的记录和日期小于某些" dd / mm / yyy"但与此同时,此记录的任何条目都没有大于table1中的相同日期。像这样的东西
------------SQL TABLE1 -----------
date | IDkey | user_name | address
04/15/2015 | 111111 | Jean Gray | 555 Bergen St.
15/04/2015 | 112233 | Homer Simposn | 123 Main St.
10/04/2015 | 112233 | Homer Simpson | 455 XYZ Ave.
04/03/2015 | 123456 | Peter Griffin | 780 111th St.
04/02/2015 | 654321 | Charles Xavier | 555 Bergen St.
04/12/2014 | 112233 | Homer Simpson | 123 Main St.
04/11/2014 | 654321 | Charles Xavier | 555 Bergen St.
04/11/2014 | 111111 | Jean Gray | 555 Bergen St.
------------SQL TABLE2 -----------
IDkey | user_name | address
112233 | Homer Simposn | 123 Main St.
123456 | Peter Griffin | 780 111th St.
654321 | Charles Xavier | 555 Bergen St.
111111 | Jean Gray | 555 Bergen St.
SELECT table2.idkey, table1.idkey, table1.date, table1.username
From table1,table2
Where table2.idkey=table1.idkey
And table1.date < DateValue('01/04/2015')
AND table1.date IS NOT > DateValue('01/04/2015');
必须不是结果中的重复值
此查询的结果必须是:
------------RESULT-----------
IDkey | user_name | address
123456 | Peter Griffin | 780 111th St.
654321 | Charles Xavier | 555 Bergen St.
答案 0 :(得分:0)
使用NOT EXISTS,如下所示:
SELECT table2.idkey, table1.idkey, table1.date, table1.username
From table1 INNER JOIN table2 ON table2.idkey=table1.idkey
Where table1.date < DateValue('01/04/2015')
AND NOT EXISTS (SELECT 1 FROM table1 t1 WHERE t1.date > DateValue('01/04/2015'));
并使用连接
答案 1 :(得分:0)
我可能会遇到语法问题,但请尝试以下方法:
SELECT Distint t2.IDKey, t2.user_name, t2.address
from table2 t2
join table1 t1
on t1.IDkey = t2.IDKey
where
( t1.[date] < DateValue('01/04/2015') and t1.[date] !> DateValue('01/04/2015'))