终于有分提问了,问题如下:
 
  我copy了600+条学生数据到我的tb_Student里面,但因为需要,我的tb_Student表里面有
外键:
  classId 班级编号
  postId  职位编号--外键,1是班长,2是组长,3是组员....
  teamID  小组编号--这个不是外键,int 类型
那么问题就来了,我可能自己再在行记录里面去给添加对应的班级编号跟小组编号吧于是:求如何用sql语句将600学生分班(update classid),班里再分组(update teamId)
一个班25人,一个组5人,每组的第一个人是组长(update postId=2)
因为是外键是吧,如果我的tb_Class里面只有id最大才20,那么能不能在这条sql语句中再加一个判断
当tb_Class没有就先在tb_Class里增加一列,name之类的可以为空,tb_Class里的termId也是外键,默认为1的
嗯,不知道我说的大家能看明白没?不明白的我跟帖继续解释呜呜,我分不多,在线等了。

解决方案 »

  1.   

    如果我的tb_Class里面只有id最大才20,那么能不能在这条sql语句中再加一个判断 
    当tb_Class没有就先在tb_Class里增加一列,name之类的可以为空,tb_Class里的termId也是外键,默认为1的 
    嗯,不知道我说的大家能看明白没? 
    没明白
      

  2.   

    那好,新招了600个学生现在要分班 
    25人一个班
    一个班5个小组
    5人一组
    分到这组的第一个人是组长 比如a-y(25个字母)分到Z班,那么a就是一组的组长,teamID=1,postID=2,classId=1(z班的id)
    可由于,原本学校只有20个班是吧,即还有100个学生没班去
    这个时候就再添加4个班(insert into tb_Class)
    然后再继续分班分组...
      

  3.   

    插入600条数据时,分别插入班级ID和小组ID?
      

  4.   

    没看明白。 (不要高估你的汉语表达能力或者我的汉语理解能力)
       建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
       参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
       
       1. 你的 create table xxx .. 语句
       2. 你的 insert into xxx ... 语句
       3. 结果是什么样,(并给以简单的算法描述)
       4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
       
       这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。   
      

  5.   


    sqlServer 2005 完成不了吗?表结构?
    好吧。
    tb_Student
    id int not null --主键,自动增长
    name varchar(50) not null --姓名
    cardId varchar(50) not null --身份证号码
    sex varchar(50) not null--性别
    ....---省略几个完全不相关字段
    classId int not null--班级编号 外键
    postId int not null--职务编号 外键
    teamId int not null--小组编号 
    希望录入的数据是:
    1 张三 430521198802025847 男 2(classId)2(postId)2(teamId)
    如今的数据是:
    1 张三 430521198802025847 男 2(classId)2(postId) 1(默认值为1)
    需求我想我已经说的很清楚了 
    为什么不可以用一条sql语句呢?
    这种循环添加不能实现?
    2个循环外加一个if判断....sql真的不能??
      

  6.   


    插入的时候是copy另外一个数据库的,而那个数据库里的student表是没有teamID这个字段的如今我要添加这个字段,该怎么添加
      

  7.   

    实在没办法。
    你们难道实际中没有碰到过这种情况?
    某个表的数据需要copy,但这个表里又有你自己新添的字段
    那么当你copy了1000条数据,这个你新添的字段怎么办??
    难道一条一条的加??
    特别是这个字段还很有规律!那么我只能自己这样写了
    select top 5 * from tb_Student where classId=1 and teamId is null order by newId()
    --先随机获得1班的5名学生
    然后再对这5名学生进行修改!将他们的teamId都改成1!
    然后再这样5次..分别改成2 3 4 5呼...
    sql语句中的逻辑判断我不会啊
    只知道有if while until怎么用的不会啊
    哪位大大给我这方面的资料哦
      

  8.   

    --> 测试数据: tb
    if object_id('[tb]') is not null drop table [tb]
    if object_id('[ntb]') is not null drop table ntb
    if object_id('tempdb..#') is not null drop table #
    go
    create table [ntb] (classid int,groupid int,name varchar(100))
    create table [tb] (name varchar(100))
    insert into tb
    select '李发生的四' union all
    select'h阿飞s四'  union all
    select '李说对方四' union all
    select'李四' union all
    select '王给大哥五' union all
    select 'aa五' union all
    select '王发五' union all
    select 'gr五' union all
    select '得分赵六' union all
    select '赵发六' union all
    select '得分' union all
    select '发' union all
    select '的' union all
    select'天'  union all
    select '李说短四' union all
    select'李四' union all
    select '发' union all
    select 'aa五' union all
    select '发发五' union all
    select 'gr五' union all
    select 'igz分赵六' union all
    select '哦发六' union all
    select '啊分'
    --> 测试数据: [arrange]
    select id=identity(int,0,1),* into # from tb order by newid()
    --select * from #
    insert into  ntb select id/10+1 as id1,id/5+1 as id2,name from #
    select * from ntb
    go
    /*classid     groupid     name
    ----------- ----------- ----------------------------------------------------------------------------------------------------
    1           1           gr五
    1           1           李说短四
    1           1           哦发六
    1           1           h阿飞s四
    1           1           王给大哥五
    1           2           gr五
    1           2           的
    1           2           igz分赵六
    1           2           aa五
    1           2           得分
    2           3           天
    2           3           李说对方四
    2           3           王发五
    2           3           发发五
    2           3           李四
    2           4           李四
    2           4           得分赵六
    2           4           发
    2           4           aa五
    2           4           李发生的四
    3           5           发
    3           5           啊分
    3           5           赵发六
    */
    不知道你分组的情况是否跟这类似?
      

  9.   

    首先谢谢你了!辛苦写了这么多
    可我没看懂...
    比如:
    select id=identity(int,0,1),* into # from tb order by newid()
    这句什么意思?
    insert into  ntb select id/10+1 as id1,id/5+1 as id2,name from #
    select * from ntb
    也不是很清楚 
    select *我知道,但# from我就迷茫了
    还有最前面的
    if object_id('[tb]') is not null drop table [tb]
    if object_id('[ntb]') is not null drop table ntb
    if object_id('tempdb..#') is not null drop table #
    不明白。。
    不过我要的就是这个结果!!!
    如果给我再详细解释下就万分感激了!