MySQL 检索条件唯一且真实的记录,即不存在其他记录
我有一个遗留数据库,正在使用基本的列布局,如下所示:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们可以首先选择您尝试排除的所有行,然后使用
NOT EXISTS
跳过该查询中的CompanyID
。We could start by selecting all of the rows that you are trying to exclude, and then use a
NOT EXISTS
to skip overCompanyID
s that are in that query.