SQL 可以找到缺少的匹配项吗?

发布于 2025-01-17 08:03:26 字数 602 浏览 2 评论 0原文

我有三个数据库:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

没有心的人 2025-01-24 08:03:26

当我们编写查询 SELECT * FROM table1 LEFT JOIN table2 on table1.id = table2.id 时,无论 table2 中是否存在匹配项,我们都将从 table1 中获取所有行。对于不匹配的行,table2 中的所有值都将为空。
这意味着我们可以使用测试 WHERE table2.idIS 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.idIS NULL.
(Obviously you will replace table2.id with the column that you join on.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文