两表关联:表   A   的字段   ID1   =   表B的字段   ID 2A: ID1,title,Name,keywordB: ID2,title ,content A.ID1   =   B.ID2删除A表中title,Name,keyword三个字段完全相同的记录,然后删除B表ID2中不存在于A表ID1中的记录,如何写SQL语句

解决方案 »

  1.   


    delete from a
    where exists (select 1 from a as tb where tb.title=a.title 
                  and tb.Name=a.Name and a.keyword=tb.keyword
                  and tb.id1<>a.id1)delete from b
    where id2 not in (select id1 from a)
      

  2.   

    三个字段完全相同的是全部删除还是保留一个?
    --A表全部删除相同的delete from A t where (select count(*) from A where name = t.name and title = t.title and keyword = t.keyword) > 1--A表保留一条重复记录delete t
    from A t
    where exists (select 1 from A where name = t.name and title = t.title and keyword = t.keyword and id > t.id)
    --删除A表记录后删除B表相关记录delete b
    from B b
    where not exists (select 1 from A where id1 = b.id2)
      

  3.   

    还有一个问题,当A表: ID1,title,Name,keyword三个字段,title,Name有几条相同重复的记录,重复的记录中,有记录keyword字段为空,另有记录keyword字段不为空,删除空记录,保留一条keyword字段不为空记录
      

  4.   

    delete from a
    where id1 not in 
    (select min(id1) from a as tb where tb.title=a.title 
                  and tb.Name=a.Name and a.keyword<>'')
    delete from b
    where id2 not in (select id1 from a)