我的目标是取出重复生日字段值的记录
sql如下
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但是发现很多的字段值为空的记录被选出,如何去掉
后来我加了is not null 但是发现还是很多的空记录被选出,sql如下
select distinct a.id,a.name,a.birthdate
from a t1, a t2
where
 t1.birthdate is not null and  t2.birthdate is not null and
 t1.birthdate=t2.birthdate
and t1.id != t2.id
order by a.birthdate
请问如何改写 sql 让很多的空记录如何去掉?

解决方案 »

  1.   

    birthdate 是什么类型的字段?show create table a 看一下你的表结构。
      

  2.   

    select * from tb_name a where exists (select 1 from tb_name b where b.birthdate=a.birthdate and b.id<>a.id) ordr by a.birthdate
      

  3.   

    若你还想在重复的记录中只取出最大ID值的那条,则这样:
    select * from tb_name a where exists (select 1 from tb_name b where b.birthdate=a.birthdate and b.id <>a.id) and id=(select max(id) from tb_name c where c.birthdate=a.birthdate) ordr by a.birthdate
      

  4.   

    select * from tb_name a ,sdfd  b where
    a.id=b.id
    ??---
     exists (select 1 from tb_name b where b.birthdate=a.birthdate and b.id <>a.id) ordr by a.birthdate
    我必须另外加1个表 如何改写这个sql
      

  5.   

    很奇怪 t1.birthdate is not null and  t2.birthdate is not null and 
    我已经加了限制条件
    但是结果却是还是有空的字段值  , 很奇特??
      

  6.   


    问题就在这儿, NULL <> '' <> '    'select distinct a.id,a.name,a.birthdate
    from a t1, a t2
    where t1.birthdate is not null 
    and t2.birthdate is not null 
    and trim(t1.birthdate) != ''
    and trim(t2.birthdate) != ''
    and t1.birthdate=t2.birthdate
    and t1.id != t2.id
    order by a.birthdate
    当您的问题得到解答后请及时结贴.
    http://topic.csdn.net/u/20090501/15/7548d251-aec2-4975-a9bf-ca09a5551ba5.html
      

  7.   

    晕,你指的是NULL值还是''值呢?如果是包含有''值的话,对null与''值进行过滤:select distinct a.id,a.name,a.birthdate
    from a t1, a t2
    where trim(ifnull(t1.birthdate,''))<>''    
        and trim(ifnull(t2.birthdate,''))<>''
        and t1.birthdate=t2.birthdate
    and t1.id != t2.id
    order by a.birthdate