假设我为同一条记录分配了两个不同的 ID。例如
RecordID | ID1 | ID2 -------------------- 1 | X | A 2 | X | B 3 | Y | C 4 | Y | C 5 | Y | D 6 | Z | E 7 | Z | E
Now, I want to get the records where ID1
is assigned to the same value where as ID2
is assigned to a different value.
For example, I want to get:
1, X, A
2, X, B
这里 ID1 分配了 X,而 ID2 分配了 A 和 B,两个不同的值。
是否可以在 SQL 或 SQL Server 中编写返回此类记录的查询?
最佳答案
您需要使用子查询,对于每一行,您都可以浏览表并查看是否有任何其他行符合与其相关的特定条件。
伪sql:
select
t1.id,
t1.variable,
t1.value
from
table t1
where
exists ( select 1
from t2
where t2.id != t1.id
and t2.variable == t1.variable
and t2.value != t1.value)
关于用于查找数据匹配或实体解析误报的 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14739134/