MySQL 检索条件唯一且真实的记录,即不存在其他记录

发布于 2024-11-18 20:58:21 字数 1377 浏览 2 评论 0原文

我有一个遗留数据库,正在使用基本的列布局,如下所示:

SampleID
CompanyID
CompanyApplication
CompanyTest1
...
CompanyTest25

至少没有标准化,这导致了一些数据挖掘问题。

我需要获取 CompanyID(分组依据),其中 CompanyApplication 字段 =“注册”,但该公司没有为任何 SampleID 选择任何 CompanyTest(n) 字段。

问题是,有些 CompanyID 同时具有 CompanyApplication 和 CompanyTest(n) 记录(多行),但我想获取仅具有注册的 CompanyApplication 的 CompanyID。

有助于说明的数据:

SampleID | CompanyID | CompanyApplication | CompanyTest1 | ... | CompanyTest25
------------------------------------------------------------------------
1         | 1        | Registration       |              |     |
------------------------------------------------------------------------
2         | 1        |                    | True         |     |
------------------------------------------------------------------------
3         | 2        | Registration       |              |     |
------------------------------------------------------------------------
4         | 2        | Registration       |              |     |
------------------------------------------------------------------------
5         | 3        |                    | True         |     |
------------------------------------------------------------------------
6         | 3        |                    |              |     | True

我只想检索第 3 行和第 4 行,因为它们只有注册而没有其他测试。

I have a legacy database that I am working with a basic column layout as such:

SampleID
CompanyID
CompanyApplication
CompanyTest1
...
CompanyTest25

Not normalized in the least, this is causing a bit of a data mining issue.

I need to get the CompanyIDs (Grouped By) where the CompanyApplication field = "Registration" but this company has none of the CompanyTest(n) fields selected for any SampleID.

The problem is, there are CompanyIDs that have both CompanyApplication and CompanyTest(n) records (multiple rows), but I want to get CompanyIDs that only have a CompanyApplication of Registration.

Data to help illustrate:

SampleID | CompanyID | CompanyApplication | CompanyTest1 | ... | CompanyTest25
------------------------------------------------------------------------
1         | 1        | Registration       |              |     |
------------------------------------------------------------------------
2         | 1        |                    | True         |     |
------------------------------------------------------------------------
3         | 2        | Registration       |              |     |
------------------------------------------------------------------------
4         | 2        | Registration       |              |     |
------------------------------------------------------------------------
5         | 3        |                    | True         |     |
------------------------------------------------------------------------
6         | 3        |                    |              |     | True

I only want to retrieve rows 3 and 4 because they ONLY have Registration and no other testing.

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

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

发布评论

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

评论(1

dawn曙光 2024-11-25 20:58:21

我们可以首先选择您尝试排除的所有行,然后使用 NOT EXISTS 跳过该查询中的 CompanyID

SELECT DISTINCT ct1.company_id
FROM company_table AS ct1
WHERE ct1.CompanyApplication = "Registration"
  AND NOT EXISTS(
        SELECT 1
        FROM company_table AS ct2
        WHERE ct1.company_id = ct2.compnay_id
          AND (ct2.CompanyTest1 IS NOT NULL
               OR ct2.CompanyTest2 IS NOT NULL
               ...
               OR ct2.CompanyTest25 IS NOT NULL)
        )

We could start by selecting all of the rows that you are trying to exclude, and then use a NOT EXISTS to skip over CompanyIDs that are in that query.

SELECT DISTINCT ct1.company_id
FROM company_table AS ct1
WHERE ct1.CompanyApplication = "Registration"
  AND NOT EXISTS(
        SELECT 1
        FROM company_table AS ct2
        WHERE ct1.company_id = ct2.compnay_id
          AND (ct2.CompanyTest1 IS NOT NULL
               OR ct2.CompanyTest2 IS NOT NULL
               ...
               OR ct2.CompanyTest25 IS NOT NULL)
        )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文