我有两张桌子
第一个叫做Event
date id
2010-04-20 97019
2010-04-20 28617
2010-04-20 96698
2010-04-20 38601
2010-05-20 96835
2010-05-21 28221
2010-05-21 4432
2010-05-21 96898
2010-05-21 4521
... ...
第二个是注册
company id2
abcd 4521
abcde 96898
abcdef 3452
abcdefg 3489
abcdefgh 66855
我想要做的是检查我的登记册中的公司没有参加活动的日期。所以我希望我的结果看起来像这样:
结果
company ids date, didn't attend
abcd 4521 2010-04-20
abcd 4521 2010-05-20
abcdefgh 66855 2010-04-20
abcdefgh 66855 2010-05-20
abcdefgh 66855 2010-05-21
... ... ...
我正在使用SQL / Access来尝试完成此任务。
SELECT * FROM Register
WHERE Register.id2
NOT IN
(SELECT Event.id FROM Event);
向我提供我注册中不是公司的所有活动参与者。
SELECT * FROM Register r
LEFT JOIN Event e
ON e.id=r.id2
WHERE r.id2 IS NULL;
什么都不给我......
关于我做错的任何想法?
答案 0 :(得分:1)
我相信这就是你要找的东西。我需要使用VBA来解决这个有趣的查询。我认为可以将其更改为一些查询和子查询,但我会将其作为练习留给您。您需要将此代码复制到模块中并使用立即窗口进行测试,例如:?RegistrarAttended。请告诉我它是怎么回事。
Public Function RegistrarAttended()
Dim db As DAO.database
Set db = CurrentDb
Dim qd As DAO.QueryDef
Set qd = db.CreateQueryDef("")
qd.sql = "SELECT edate from Event GROUP BY edate" ' gather unique dates
Dim rsDistinctDates As DAO.recordSet
Set rsDistinctDates = qd.OpenRecordset()
Dim rsReg As DAO.recordSet
Set rsReg = db.OpenRecordset("SELECT * FROM Register") 'get contents of Register table
Dim rsCP As DAO.recordSet
Dim qdCP As New DAO.QueryDef
Set qdCP = db.CreateQueryDef("")
' create a cartesian product of Register and Event table that requires 2 parameters: event-date and id
qdCP.sql = "SELECT DISTINCT Register.id2, Register.company, Event.id, Event.edate" & _
" FROM Register, Event" & _
" WHERE (((Event.id)=[inpid]) AND ((Event.edate)=[inpdate]));"
Debug.Print "Company", "id", "Event Date"
Do Until rsDistinctDates.EOF
Do Until rsReg.EOF
qdCP![inpdate] = rsDistinctDates("edate")
qdCP![inpid] = rsReg![id2]
Set rsCP = qdCP.OpenRecordset
If rsCP.RecordCount = 0 Then
Debug.Print rsReg![company], rsReg![id2], rsDistinctDates![edate]
End If
rsReg.MoveNext
Loop
rsReg.MoveFirst
rsDistinctDates.MoveNext
Loop
End Function