1

我需要找出哪些司机在过去 6 个月内没有购买燃料。

我有两个表 TransactionReport(过去 6 个月的所有交易)和 DriverList(所有驱动程序)

TransactionReport
DriverID  |  DriverLastName | DriverFirstName | Other Columns not used for this query

DriverList
DriverID  |  DriverLastName | DriverFirstName | Other Columns not used for this query

我想返回未在 TransactionReport 中列出的 DriverList 中列出的所有 DriverID。

结果应该给我以下

DriverID  |  DriverLastName | DriverFirstName
4

4 回答 4

3

在右表中仅获得 NULL 结果的左外连接应该可以工作

select distinct driverlist.driverid, driverlist.driverlastname, driverlist.driverfirstname
from driverlist
left outer join transactionreport
on driverlist.driverid = transactionreport.driverid
where transactionreport.driverid is null
于 2012-10-26T15:28:46.177 回答
2
select 
    DriverID, DriverLastName, DriverFirstName

from
    DriverList

where
    DriverID not in (select DriverID from TransactionReport)
于 2012-10-26T15:36:27.763 回答
1
SELECT dl.DriverID , dl.DriverLastName, dl.DriverFirstName FROM DriverList dl
 LEFT JOIN TransactionReport tr ON  dl.DriverID  = tr.DriverID  
WHERE tr.DriverID IS NULL
于 2012-10-26T15:27:43.623 回答
0

你可以像前面提到的那样做:

Select dl.DriverID , dl.DriverLastName, dl.DriverFirstName From DriverList dl
 LEFT Join TransactionReport tr on  dl.DriverID  = tr.DriverID  
WHERE tr.DriverID is Null

哪个会起作用。但是,使用反半联接可以获得更快的结果和更易读的查询。似乎更高版本的 MS Access 支持这一点:

SELECT dl.DriverID, dl.DriverLastName, dl.DriverFirstName From DriverList dl
WHERE NOT EXISTS (
     SELECT tr.DriverID 
     FROM TransactionReport tr 
     WHERE tr.DriverID = dl.DriverID)

这里的关键是要注意EXISTS语句里面的WHERE。NOT 逻辑已经在 EXISTS 前面,因此您必须在语句中测试是否相等。

于 2012-10-26T15:41:52.247 回答