连接两个表,然后从第三个表中提取不匹配的记录

发布于 2024-11-05 22:45:28 字数 358 浏览 0 评论 0原文

我有 Visual Studio 2005,并且正在用 VB 而不是 C+ 编写代码。我需要一个 Select 语句,并找到了一些接近但不适合我的情况的语句。我有三个表:

PROJECT
    [Projnum]
    [ShipDate] 

CUSTOMER
    [Projnum]
    [Jobnum]

TAGS
    [Jobnum] 

我需要连接 CUSTOMER 和 PROJECT,以便我知道 PROJECT.ShipDate 为空的所有 CUSTOMER.Jobnum 记录。然后,我需要从这些记录中获取哪些记录与 TAGS.Jobnum 不匹配。

任何帮助将不胜感激。非常感谢,查克。

I have Visual Studio 2005 and am writing code in VB, not C+. I need a Select statement and have found some that are close but not for my situation. I have three tables:

PROJECT
    [Projnum]
    [ShipDate] 

CUSTOMER
    [Projnum]
    [Jobnum]

TAGS
    [Jobnum] 

I need to join CUSTOMER and PROJECT so I know all the CUSTOMER.Jobnum records where PROJECT.ShipDate is null. Out of those records, I then need to get which ones do not have a match from TAGS.Jobnum.

Any help would be much appreciated. Thanks a bunch, Chuck.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

仄言 2024-11-12 22:45:28
SELECT c.Jobnum
    FROM customer c
        INNER JOIN project p
            ON c.Projnum = p.Projnum
    WHERE p.ShipDate IS NULL
        AND NOT EXISTS(SELECT NULL FROM tags t WHERE t.Jobnum = c.Jobnum)
SELECT c.Jobnum
    FROM customer c
        INNER JOIN project p
            ON c.Projnum = p.Projnum
    WHERE p.ShipDate IS NULL
        AND NOT EXISTS(SELECT NULL FROM tags t WHERE t.Jobnum = c.Jobnum)
南城追梦 2024-11-12 22:45:28

如果我理解正确的话:

PROJECT
  ProjNum
  ShipDate

CUSTOMER
  ProjNum
  JobNum

TAGS
  JobNum

您想要所有未发货的项目:

SELECT c.JobNUm
FROM Project p
  INNER JOIN Customer c
    ON c.ProjNum = p.ProjNum
WHERE p.ShipDate is null

然后您想要标签表中没有 JobNum 的项目:

SELECT c.JobNUm
FROM Project p
  INNER JOIN Customer c
    ON c.ProjNum = p.ProjNum
WHERE p.ShipDate is null
  AND c.JobNum NOT IN (SELECT JobNum from TAGS)

可以做得更简单,但我想展示我的工作。

If I understand correctly:

PROJECT
  ProjNum
  ShipDate

CUSTOMER
  ProjNum
  JobNum

TAGS
  JobNum

And you want all unshipped projects:

SELECT c.JobNUm
FROM Project p
  INNER JOIN Customer c
    ON c.ProjNum = p.ProjNum
WHERE p.ShipDate is null

And then you want the ones without a JobNum in the TAGS table:

SELECT c.JobNUm
FROM Project p
  INNER JOIN Customer c
    ON c.ProjNum = p.ProjNum
WHERE p.ShipDate is null
  AND c.JobNum NOT IN (SELECT JobNum from TAGS)

Could be done simpler, but I wanted to show my work.

梦归所梦 2024-11-12 22:45:28

LEFT JOIN 可能比 NOT INNOT EXISTS 更快。

SELECT c.JobNUm
FROM Project p
  INNER JOIN Customer c
    ON c.ProjNum = p.ProjNum
  LEFT JOIN tags t ON t.jobnum=c.jobnum
WHERE p.ShipDate is null AND t.jobnum IS NULL

A LEFT JOIN may be faster than NOT IN and NOT EXISTS.

SELECT c.JobNUm
FROM Project p
  INNER JOIN Customer c
    ON c.ProjNum = p.ProjNum
  LEFT JOIN tags t ON t.jobnum=c.jobnum
WHERE p.ShipDate is null AND t.jobnum IS NULL
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文