表结构如下:
姓名   班级    迟到次数   日期
张三      1     1         2010/1/2
张三      1     2         2010/3/2
张三     1      3        2010/6/3
李四    2      1          2010/9/5
迟到次数是累计值,张三再次迟到的话,要插入(张三,1,4,日期值)
现在只有张迟到人员的名单,只有 姓名和班级迟到名单
姓名  班级
张三    1
王五    3
如何把上面的两条数据插入数据库中,如何获得数据库中迟到次数的最大值。
insert into Table values ('张三',1,这里该怎么写?,getdate())
insert into Table values ('王五',1,这里该怎么写?,getdate())

解决方案 »

  1.   

    insert into Table values ('王五',1,null,null,getdate())
    insert into Table (姓名, 班级,日期)
    values ('王五',1,getdate())
      

  2.   

    insert into Table
    select 姓名,班级,count(*),getdate()
    from 迟到名单
    group by 姓名,班级,getdate()
      

  3.   

    insert #tb
    select '张三', 1,max(迟到次数)+1,getdate() from #tb
      

  4.   

    上面看错了if not exists(select 1 from Table  where  name ='zz')
    begin
     insert into Table (姓名, 班级,日期)
    values ('王五',1,getdate())
    end
    else
    begin
     update table 
    set 迟到次数 = 迟到次数+1
    end
      

  5.   


    if not exists(select 1 from Table  where  name ='zz')
    begin
     insert into Table (姓名, 班级,日期)
    values ('zz,1,getdate())
    end
    else
    begin
     update table 
    set 迟到次数 = 迟到次数+1,日期=getdate()
    where name  ='zz' 
    end
      

  6.   

    insert into Table
    select t1.姓名,t1.班级,max(t2.迟到次数)+1,getdate()
    from 迟到名单 t1
    left join Table t2
    group by 姓名,班级,getdate()
      

  7.   


    insert into Table
    select t1.姓名,t1.班级,max(t2.迟到次数)+1,getdate()
    from 迟到名单 t1
    left join Table t2
    group by 姓名,班级,getdate()
      

  8.   

    在数据库设计的时候改成默认值为0,就不用插入迟到次数了。select max(迟到次数字段名) from 表名
      

  9.   


    --更正,最终答案
    insert into Table
    select t1.姓名,t1.班级,isnull(max(t2.迟到次数),0)+1,getdate()
    from 迟到名单 t1
    left join Table t2
    group by 姓名,班级,getdate()
      

  10.   


    create table a(姓名varchar(10),班级int,迟到次数int,日期date)
    insert into a
    select 'aa',1,1,'2010-11-10'create table b(姓名varchar(10),班级int)
    insert into b
    select 'aa',1
    union
    select 'bb',2insert into a
    select t1.姓名,t1.班级,isnull(max(t2.迟到次数),0)+1,getdate()
    from b t1
    left join a t2 on t1.姓名=t2.姓名
    group by t1.姓名,t1.班级select * from a/*
    姓名 班级 迟到次数 日期
    aa 1 1 2010-11-10
    aa 1 2 2010-11-11
    bb 2 1 2010-11-11
    */