sql - Oracle 为什么存在查询返回此结果
问题描述
我很困惑使用存在来比较两个表之间的两个数据。我期望从查询中获得 0 行,但它返回一些数据。我不知道为什么。
select main_rule, child_rule from scoredef_detail s where s.main_rule = 515;
1 515 516
2 515 517
select main_rule, child_rule from scoredef_detail_at s where s.main_rule = 515;
1 515 516
2 515 517
此查询返回上面的所有内容。我试图找出差异。这些列是其他一些表的外键。它可以与其他表的记录相关吗?
select main_rule, child_rule
from scoredef_detail s
where exists
(select main_rule, child_rule
from scoredef_detail_at sa
where s.main_rule = sa.main_rule and (s.child_rule <> sa.child_rule));
提前致谢。
解决方案
您可以使用 afull join
来获取不匹配:
select s.*, sa.*
from (select main_rule, child_rule
from scoredef_detail s
where s.main_rule = 515
) s full join
(select main_rule, child_rule
from scoredef_detail_at s
where s.main_rule = 515
) sa
on sa.main_rule = s.main_rule and sa.child_rule = s.child_rule
where sa.main_rule is null or s.main_rule is null;
这将显示在一个表中但不在另一个表中的对。
推荐阅读
- php - 如果字段失败,提交后在 laravel 中动态保持数据表单
- windows - 工作线程不屈服于主线程
- c - 如何让函数在 C 中返回自己的类型?
- python - (haystack + elasticsearch) {{ result.object.get_absolute_url }} 不起作用(我有 get_absolute_url() 方法)
- android - 如何在 Android 11 的后台查看蓝牙的状态(开/关)?
- c - c语言中的TCP客户端/服务器
- c - 如何将库头添加到公共范围
- javascript - 将 EJS 上呈现的数组传递给 Chart.js
- assembly - 将指令数修改为一条
- python - 找不到 numpyro.render_model