表A
id   names
1     笔记本
2     作业本
3     课本
4     笔记本
5     作业本
6     笔记本
如上,我要把names字段中重复的个数大于3个的去掉,得到如下结果:
id   names
2     作业本
3     课本
5     作业本
sql语句改怎么写?
sql数据库

解决方案 »

  1.   

    select *
    from tb 
    where names not in(select names from tb group by names having count(*)>=3)
      

  2.   

    create table #tb(id int,  names varchar(10))
    insert into #tb
    select 1,'笔记本'
    union all select 2,'作业本'
    union all select 3,'课本'
    union all select 4,'笔记本'
    union all select 5,'作业本'
    union all select 6,'笔记本'select *
    from #tb 
    where names not in(select names from #tb group by names having count(*)>=3)
    drop table #tb
    /*
    id   names
    ------------------------
    2 作业本
    3 课本
    5 作业本
    */