sql - 过滤结果的 SQL 问题
问题描述
我目前得到的结果如下所示:
Row | ParentLevel | ChildLevel |
1 | 1 | 1 |
2 | 2 | 2 |
3 | 2 | 3 |
如果没有不匹配的结果,我只希望在父子匹配时出现一行。
在这种情况下,第 1 行很好,因为它是父级别 1 唯一出现的时间
第 2 行我不想出现,因为第 3 行在那里,并且父母和孩子在第 3 行不匹配。
这可以在 where 语句中设置吗?
解决方案
您可以使用not exists
:
select t.*
from t
where t.ParentLevel <> t.ChildLevel or
not exists (select 1
from t t2
where t2.ParentLevel = t.ParentLevel and t2.ParentLevel <> t2.ChildLevel
);
推荐阅读
- c++ - 如何在 VSCode 中更改 C++ 文件中类似 jsdoc 的标签的突出显示颜色
- excel - Excel Solver - 单位 5 后每份的价格变化
- python - 从文件中选择性打印输出
- tensorflow - 将张量输入 CNN 时我应该转置它吗
- ios - iOS NFC 显示时间
- javascript - 每当我使用 JavaScript 更新任何样式或从 DOM 添加/删除节点时,“渲染树”或“DOM/CSSOM”会发生什么变化?
- angular - Angular CLI 可以配置为仅在保存时实时重新加载吗?
- matlab - 如何在LabVIEW中实现stepinfo的功能?特别是 settlingMax 和 settlingMin?
- php - 更改关联变量后会话值更改
- c++ - MFC 中的 C++ UTF-8/ASCII 到 UTF-16