order by 1,2,3 ?
是这个意思吗

解决方案 »

  1.   

    SubjectName 这个值从那里来?
      

  2.   

    --处理示例--测试数据
    create table 表(ID int,GradeID int,SubjectName varchar(10))
    insert 表 select 1,1,'aa'
    union all select 2,1,'aa'
    union all select 3,1,'aa'
    union all select 4,2,'aa'
    union all select 5,2,'aa'
    union all select 6,2,'aa'
    union all select 7,2,'aa'
    union all select 8,3,'aa'
    union all select 9,3,'aa'
    go--更新处理
    update 表 set SubjectName=b.name
    from 表 a join(
    select id=1,name='数学'
    union all select 2,'语文'
    union all select 3,'英语'
    union all select 4,'物理'
    )b on b.id=(
    select count(*) from 表
    where GradeID=a.GradeID
    and id<=a.id)
    go--显示更新结果
    select * from 表
    go--删除测试
    drop table 表/*--测试结果ID          GradeID     SubjectName 
    ----------- ----------- ----------- 
    1           1           数学
    2           1           语文
    3           1           英语
    4           2           数学
    5           2           语文
    6           2           英语
    7           2           物理
    8           3           数学
    9           3           语文(所影响的行数为 9 行)
    --*/
      

  3.   

    还是有点问题!
    我按如下修改
    update subject set name=b.name1
    from subject a join(
    select id=1,name1='数学'
    union all select 2,'语文'
    union all select 3,'英语'
    union all select 4,'物理'
            union all select 5,'化学'
            union all select 6,'政治'
            union all select 7,'生物'
            union all select 8,'几何'
    )b on b.id=(
    select count(*) from subject
    where GradeID=a.GradeID
    and id<=a.id) 
    产生结果如下:
    ID       GradeID   Name
    123 0 数学
    126 0 语文
    34 1 数学
    28 2 语文
    30 2 英语
    1 2 数学
    43 2 物理
    71 2 化学
    72 3 物理
    67 3 英语
    11 3 数学
    36 3 语文
    79 3 化学
    38 4 语文
    19 4 数学
    当GradeID=2时候就出现问题了
      

  4.   

    替换顺序是按id排序,而不是按记录顺序,SQL中没有记录顺序的慨念