2

我需要比较查询的同一个表中的行,并返回另一个表中 id 匹配记录的结果。

这是第一个表的示例:

id    checkin     checkout 
1     01/15/13    01/31/13   
1     01/31/13    05/20/13
2     01/15/13    05/20/13
3     01/15/13    01/19/13
3     01/19/13    05/20/13
4     01/15/13    02/22/13
5     01/15/13    03/01/13

我只想获取在今天日期之后没有匹配记录的 id 表中的记录。所以 ID 的 1 和 3 不会被返回,因为第二条记录的结帐日期在今天之后。然后我想将这些 id 与另一个表中的记录匹配,并从第二个表中返回值。

示例第二个表:

id    address     zip
1     abc         98734
2     uvx         12345
3     ;alksdf     12347
4     bhg         34567
5     ;alkjdf     56789

所以在这种情况下,我会返回 id 4 和 5 的地址和 zip。

4

2 回答 2

3

就像是?

 SELECT *
 FROM table2 
 WHERE table2.id NOT IN (SELECT DISTINCT table1.id
                         FROM table1
                         WHERE table1.checkin > sysdate OR
                           table1.checkout > sysdate) AND
 table2.id IN (SELECT DISTINCT table1.id
               FROM table1)

编辑以排除那些日期在未来的人,并确保有相应的记录

于 2013-04-29T22:07:13.717 回答
2

to get only those records from today or earlier, do this:

where checkout <= trunc(sysdate)

Edit starts here

to exclude records with checkout dates in the future, do this:

where not exists
(select 1 record
from yourTable t2
where checkout > trunc(sysdate + 1)
and t2.id = yourTable.id
)
于 2013-04-29T22:43:27.033 回答