SQL 可以找到缺少的匹配项吗?
我有三个数据库:
1.) 人员数据库,每个人都有一个唯一的编号
2.) 发生某事的日期数据库,每个日期都有一个唯一的编号
3.) 记录谁参加了这些活动的数据库,即在同一行中包含人员编号和活动编号的表。要查询谁在任何一天去上班,我可以这样做:
SELECT "people"."last_name",
"ev_dates"."event_date",
"attendee"."id",
"attendee"."ev_id"
FROM "attendee",
"people",
"ev_dates"
WHERE "attendee"."id" = "people"."id"
AND "attendee"."ev_id" = "ev_dates"."id"
但是,我不知道如何查询没有参加活动的人。这本质上意味着第三个表中的人和事件之间不存在匹配。类似于 SELECT "PEOPLE"."LASTNAME",其中事件 ID 和人员 ID 之间不存在匹配。
如果我只是将等于更改为不等于,我会得到人们确实出现的所有其他日期的列表。
这恰好是 libreoffice 基地。
I have three databases:
1.) A database of people, each with a unique number
2.) A database of dates where something happened, each with a unique number
3.) A database of who went to these events, meaning a table with a person number and an event number in the same row. To Query who DID go to work on any given day, I can do this:
SELECT "people"."last_name",
"ev_dates"."event_date",
"attendee"."id",
"attendee"."ev_id"
FROM "attendee",
"people",
"ev_dates"
WHERE "attendee"."id" = "people"."id"
AND "attendee"."ev_id" = "ev_dates"."id"
However, I can't figure out how to query someone who DIDN'T go to an event. It would essentially mean that there is no match between a person and an event in a third table. Sort of SELECT "PEOPLE"."LASTNAME" WHERE NO MATCH BETWEEN EVENTID AND PERSONID EXISTS.
If I were to just change the equals to the not equals, I get a list of all the other days people did show up.
This happens to be libreoffice base.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当我们编写查询 SELECT * FROM table1 LEFT JOIN table2 on table1.id = table2.id 时,无论 table2 中是否存在匹配项,我们都将从 table1 中获取所有行。对于不匹配的行,table2 中的所有值都将为空。
这意味着我们可以使用测试
WHERE table2.id
IS NULL 找到不匹配的行。(显然,您将用您加入的列替换
table2.id
。)When we write a query
SELECT * FROM table1 LEFT JOIN table2 on table1.id = table2.id
we will get all the rows from table1 whether there are matches in table2 or not. All values from table2 will be null for non-matched rows.This means that we can find the non-matched rows using the test
WHERE table2.id
IS NULL.(Obviously you will replace
table2.id
with the column that you join on.)