我想返回两个+记录的birth_date相同但是person_id不相等的连接的结果。我正在使用Oracle。所以,如果我得到4个结果,其中第1行和第1行2具有相同的birth_date和不同的person_id,将返回这些行。第3行和第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;
答案 0 :(得分: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
请注意<
的使用,而不是直观的!=
。这样做是为了防止相同的组合以不同的顺序返回(例如(1,2)和(2,1))。
答案 1 :(得分:0)
您是否尝试按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