1

我想从两个以上记录的birth_date 相同但person_id 不相等的连接返回结果。我正在使用甲骨文。因此,如果我得到 4 个结果,其中第 1 行和第 2 行具有相同的出生日期和不同的 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;
4

2 回答 2

2
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

注意使用 of<而不是直观的!=. 这样做是为了防止相同的组合以不同的顺序返回(例如 (1,2) 和 (2,1))。

于 2013-11-08T16:23:52.487 回答
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
于 2013-11-08T16:27:42.723 回答