表A
id        name
1          aa
2          aa*
3          aa**
4          bb
5          cc
6          cc*表B
id         Aid         chiMa
1           1           36
2           2           36
3           3           37
4           4           40
5           4           41
6           5           40
7           6           40
8           6           39表B是表A的子表
现在出现一个问题
表A中带*的记录都是重复的数据
我现在要将表A中带*的重复记录删除
并且表B中非重复的记录也要保存
根据上表得到的结果分别为:表A
id        name
1          aa
4          bb
5          cc表B
id         Aid         chiMa
1           1           36
3           1           37
4           4           40
5           4           41
6           5           40
8           5           39哪位高手明白我的意思
帮我解决下
谢谢

解决方案 »

  1.   

    delete a from ta  a
    where exists(select 1 from ta where a.id < id)
      

  2.   

    使用触发器比较合适create trigger t1
    on ta
    for delete
    as
    begin
      delete t2
      where aid in(select id from deleted)
    enddelete t1
    where charindex('*',name)>0 
      

  3.   


    处理表A的
    delete t from A t where exists(select 1 from A where id<t.id and [name]=t.[name])
    表B的没看懂
      

  4.   

    如果*只是为了标明数据的话delete a from ta  t 
    where exists(select 1 from ta where t.id < id and name=t.name) 
      

  5.   


    --> 测试时间:2009-12-09 11:38:10
    --> 测试菜鸟:l8r
    --> 我的淘宝:《戒色坊》http://shop36766744.taobao.com/if object_id('[TA]') is not null drop table [TA]
    create table [TA]([id] int,[name] varchar(4))
    insert [TA]
    select 1,'aa' union all
    select 2,'aa*' union all
    select 3,'aa**' union all
    select 4,'bb' union all
    select 5,'cc' union all
    select 6,'cc*'
    if object_id('[TB]') is not null drop table [TB]
    create table [TB]([id] int,[Aid] int,[chiMa] int)
    insert [TB]
    select 1,1,36 union all
    select 2,2,36 union all
    select 3,3,37 union all
    select 4,4,40 union all
    select 5,4,41 union all
    select 6,5,40 union all
    select 7,6,40 union all
    select 8,6,39
    delete from TB where Aid in(select id from TA where charindex('*',name)>0)delete from TA where charindex('*',name)>0select * from [TA]select * from [TB]
    /*id          name 
    ----------- ---- 
    1           aa
    4           bb
    5           cc(所影响的行数为 3 行)id          Aid         chiMa       
    ----------- ----------- ----------- 
    1           1           36
    4           4           40
    5           4           41
    6           5           40(所影响的行数为 4 行)*/drop table [TA],[TB]
      

  6.   

    楼上的误会我的意思了
    例如表A中id为 1、2、3 的其实是同一条数据
    对应表B的记录为: 
    表B
    id         Aid         chiMa
    1           1           36
    2           2           36
    3           3           37我的思路是这样的
    先将表B中的Aid都换成1、1、1
    这样的话就是
    表B
    id         Aid         chiMa
    1           1           36
    2           1           36
    3           1           37然后再根据Aid和chiMa相同的取其最大的一个
    得到的结果为
    表B
    id         Aid         chiMa
    2           1           36
    3           1           37
      

  7.   

    但是我不知道怎么用sql语句实现
    哪位高手帮我解决下
    谢谢
      

  8.   


    update TB set Aid=(select id from TA where name=left((select name from TA where id=A.Aid),charindex('*',name+'*')-1))
    from TB Adelete t from TB T where exists(select 1 from TB where t.Aid=Aid and T.chiMa=chiMa and T.id>id)delete from TA where charindex('*',name)>0select * from [TA]select * from [TB]
    /*id          name 
    ----------- ---- 
    1           aa
    4           bb
    5           cc(所影响的行数为 3 行)id          Aid         chiMa       
    ----------- ----------- ----------- 
    1           1           36
    3           1           37
    4           4           40
    5           4           41
    6           5           40
    8           5           39(所影响的行数为 6 行)*/