sql-server - 使用一个选择语句如何获得下表结果
问题描述
在这种情况下,需要得到的输出是每个 id 的值应该只有一个值分配给该 id(col1)。如果相同的 id 具有差异值,则无需获取该值并输出。
下面是一个示例表
输入表'Demo'
col1 col2
100 A
100 A
100 A
100 A
101 A
101 B
102 A
102 B
102 B
102 C
输出
col1 col2
100 A
100 A
100 A
100 A
解决方案
您可以使用not exists
:
select d.*
from Demo d
where not exists (select 1 from Demo d1 where d1.col1 = d.col1 and d1.col2 <> d.col2);
推荐阅读
- c# - 反转一些孩子后如何保存xml文件
- javascript - 如何从角度4的数组中获取值
- javascript - Javascript:在不使用内置平方根公式的情况下测试完美平方
- less-unix - 如何有效地跳转到大型日志中的特定时间?
- c# - 如何在服务器端处理 POST Web 请求?
- arrays - RxAndroidBle,值应为十六进制
- mysql - SailsJS 1.0:Mongo 中模型的 .create() 错误,单向关联到存储在 MySQL 中的模型
- javascript - 我安装了 MongoDB,但它没有运行
- maven - mvnrepository.com 中的用法是什么意思
- sql - 为SQL中的数据列表插入多行