学历表
BigClass
id Name
01  初中
02  高中
03  大学学生表
student
id stuname grade
1   AA      初中
2   BB      高中
3   CC      大学
4   BD      高中
5   FF      大学

学生表学历它保存的是学历表的name
我想把它转为学历表的id学生表
student
id stuname grade
1   AA      01
2   BB      02
3   CC      03
4   BD      02
5   FF      03
Sql语句怎么写?

解决方案 »

  1.   

    update s set s.grade=b.id from student as s join BigClass as b on s.grade=b.Name
      

  2.   

    --> 测试数据: #BigClass
    if object_id('tempdb.dbo.#BigClass') is not null drop table #BigClass
    create table #BigClass (id varchar(11),Name varchar(11))
    insert into #BigClass
    select '01','初中' union all
    select '02','高中' union all
    select '03','大学'
    --> 测试数据: #student
    if object_id('tempdb.dbo.#student') is not null drop table #student
    create table #student (id int,stuname varchar(11),grade varchar(11))
    insert into #student
    select 1,'AA','初中' union all
    select 2,'BB','高中' union all
    select 3,'CC','大学' union all
    select 4,'BD','高中' union all
    select 5,'FF','大学'update s set s.grade=b.id from #student as s join #BigClass as b on s.grade=b.Nameselect * from #student/*
    id          stuname     grade
    ----------- ----------- -----------
    1           AA          01
    2           BB          02
    3           CC          03
    4           BD          02
    5           FF          03
    */
      

  3.   

    select a.id, a.stuname, b.id
    from student a left join BigClass b on a.grade=b.Name