完成 查找一个表里不同用户有相同的出生日期的用户
这里有2个sql
1
select distinct a.id,a.name,a.birthdate 
from a t1, a t2 
where t1.birthdate=t2.birthdate 
and t1.id != t2.id 
order by a.birthdate 
和 
2
select a.* from tt a 
inner join 
(select birthdate from tt group by birthdate having count(*)>=2) b 
on a.birthdate=b.birthdate 按照道理2者结果应该一致,但是实际上2这数据不完全一致。 
后者总是多几条  (后者总是有几个只有1条 不重复记录的结果) 
反复测试 发现 , 如何调整后者 (后者性能远远大于前者)

解决方案 »

  1.   


    select distinct a.id,a.name,a.birthdate 
    from a t1, a t2 
    where t1.birthdate=t2.birthdate 
    and t1.id != t2.id 
    order by a.birthdate 
    没有任何问题 1   2009-1-1
    2   2009-1-1
    3   2009-1-2
    4   2009-1-2
    。2select a.* from tt a 
    inner join 
    (select birthdate from tt group by birthdate having count(*)>=2) b 
    on a.birthdate=b.birthdate 
    就不是很精确 ,不知道什么原因,有时候还发现不稳定,数据总是有5-10条的波动
    当然绝大部份是好的 
    但是总有极少的几条被我艰难的发现1   2009-1-1
    2   2009-1-1
    3   2009-1-2
    4   2009-1-2
    5   2009-1-2
    6   2009-1-3
    7   2009-1-5
    8   2009-1-5。
    这里 6   2009-1-3  按照道理不因该出现的返回的记录集中
    但是结果总能发现这么几条 
    不知道原因 ,如何解决
      

  2.   

    特别是
    select a.* from tt a 
    inner join 
    (select firstname,secondname from tt group by firstname,secondname having count(*)>=2) b 
    on 
    a.firstname=b.firstname 
    and a.secondname=b.secondname
    数据明显发现不准。 当然可以忍受 ,但是不能总这样。用distinct就很精确
      

  3.   

    提供一下这两个语句的结果。select birthdate from tt 
    where birthdate='2009-1-3'
    group by birthdate having count(*)>=2;select birthdate from tt 
    where birthdate='2009-1-3';
      

  4.   

    select birthdate from tt 
    where birthdate='1974-1-1'
    group by birthdate having count(*)>=2;
    得到1条
    select birthdate from tt 
    where birthdate='1974-1-1';
    得到28条
      

  5.   

    现在是  select a.* from tt a 
    inner join 
    (select firstname,secondname from tt group by firstname,secondname having count(*)>=2) b 
    on 
    a.firstname=b.firstname 
    and a.secondname=b.secondname  不是仅仅1个字段