删除除了id号不同,其他都相同的学生冗余信息学生表(student) 如下:id号 学号 姓名 课程编号 课程名称 分数1 2005001 张三 0001 数学 692 2005002 李四 0001 数学 893 2005001 张三 0001 数学 69
SQL:

解决方案 »

  1.   


    delete from tb where id not in (select min(id) from tb group by 学号,姓名,课程编号,课程名称,分数)
      

  2.   

    delete tb from tb t where id not exists(select 1 from tb where 学号 = t.学号 and 姓名 = t.姓名 and 课程编号 = t.课程编号 and 课程名称 = t.课程名称 and 分数 = t.分数 and id < t.id)
      

  3.   


    create table student
    (id号 int,学号 varchar(9), 姓名 varchar(8), 课程编号 varchar(5), 
    课程名称 varchar(6), 分数 int)insert into student
    select 1, '2005001', '张三', '0001', '数学', 69 union all
    select 2, '2005002', '李四', '0001', '数学', 89 union all
    select 3, '2005001', '张三', '0001', '数学', 69select * from studentid号         学号        姓名       课程编号  课程名称   分数
    ----------- --------- -------- ----- ------ -----------
    1           2005001   张三       0001  数学     69
    2           2005002   李四       0001  数学     89
    3           2005001   张三       0001  数学     69
    delete a 
    from student a
    left join
    (select min(id号) kid from student
    group by 学号,姓名,课程编号,课程名称,分数) b
    on a.id号=b.kid where b.kid is nullselect * from studentid号         学号        姓名       课程编号  课程名称   分数
    ----------- --------- -------- ----- ------ -----------
    1           2005001   张三       0001  数学     69
    2           2005002   李四       0001  数学     89(2 row(s) affected)