我想从 two+ 记录的birth_date相同,但person_id不相等的连接返回结果。我正在使用甲骨文。因此,如果我得到 4 个结果,其中第 1 行和第 2 行具有相同的birth_date和不同的person_id,则将返回这些行。如果第 3 行和第 4 行具有相同的birth_date和相同的person_id,则不会返回这些行。我得到结果,但我想过滤行birth_date相等但person_id<>的结果。
select t3.field1, t6.field2, t6.field3, t3.field4, t3.field5
from table1 t1
inner join table2 t2 on t1.@matching = t2.@matching
inner join table3 t3 on t3.@matching = t1.@matching
inner join table4 t4 on t4.@matching = t1.@matching
inner join table5 t5 on t5.@matching = t4.@matching
inner join table6 t6 on t6.@matching = t3.@matching
where t1.@requirement = 'xxx'
and t2.@requirement = 'xxx'
and t2.@requirement is null
and t4.@requirement = 'xxx'
and t5.@requirement = 'xxx'
and t1.@requirement ='xxx'
and t5.@requirement is null
order by t1.@field ASC;
SELECT a.birth_date, a.id, b.id
FROM some_table a, some_table b
WHERE a.birth_date = b.birth_date
AND a.id < b.id
请注意<
而不是直观!=
的用法。这样做是为了防止相同的组合以不同的顺序返回(例如 (1,2) 和 (2,1))。
您是否尝试过按person_id对数据进行分组。这会将具有相同person_id的所有记录放入 1 条记录中
查询将如下所示
SELECT a.birth_date, a.id, b.id
FROM some_table a, some_table b
WHERE a.birth_date = b.birth_date
AND a.id < b.id
GROUP BY a.id