sql - 不等于运算符在 SQL Select 语句中不起作用
问题描述
我有两个表 A 和 B 包含相同的 Id(不是两个表中的主键),我只想获取表 A 中存在的数据。
表 A:
Id
1
2
555
6
表 B
Id
1
2
1
2
1
2
查询语句
select distinct o.Id from Table A o,Table B ot where o.isActive=1 and not(ot.Id=o.Id)
这将返回表 A 中存在的相同数据,但预期结果应为:
Id
555
6
我怎样才能得到这个作品?
解决方案
下面是一种方法。我还添加了一个条件以确保表 B 中的 Id 字段中存在合法值,因为您说它不是标识列。
select distinct Id
from Table A
where Id not in
(select Id
from Table B
where Id > 0)
推荐阅读
- macos - macOS Mojave 中的 SCNView 和处理器加载问题
- node.js - Nodejs如何为每个请求设置内容类型标头
- visual-studio-code - 转到文件在 Visual Studio Code 中不起作用
- c++ - 在选项卡控件中添加对话框
- python - 由于内存错误从文件加载时 Numpy 数组崩溃,但原始 numpy 变量是在同一环境中创建的
- mnist - 为什么我们在 tensorflow-serving 示例中使用名称“x”?
- java - 在 JAXBContext 中使用将 XML 转换为类的一些问题
- python - 如何在 Kivy 中自定义 style.kv
- c# - 在多个 SQL 查询中使用哪一个,Parallel.For 或 Tasks/async?
- r - 修复分面包裹的条形图ggplot2中的条形宽度