select intId as id,strName as 姓名, (select strCaption from baseCode where intId=p.intEducationId) as 学历,(select strCaption from basecode where intid= p.intNationId) as 民族
from person p

解决方案 »

  1.   

    select a.intId ID,a.strName 姓名,isnull(b.strCaption,'未知') 学历,isnull(c.intNationId,'未知') 民族
    from Person a left join BaseCode b on a.intEducationId=b.intId 
    left join BaseCode c on a.intNationId=c.intId
      

  2.   

    select b.intid,strname,isnull(a.strcaption,'未知') 学历,isnull(c.strcaption,'未知') 民族 from Person b left join BaseCode a on b.intEducationId=a.intId left Join BaseCode c on b.intNationId=c.intId
      

  3.   

    declare @sql varchar(8000)
    set @sql=''
    declare @Id int
    select top 1 @id=intEducationId from Person where intEducationId is not nullselect @id=intParent from Basecode 
    where intParent<>0 and intId=@idselect @sql=@sql+',isnull(b.strCaption,''未知'') as ['+strCaption+']'
    from BaseCode
    where intId=@idselect top 1 @id=intNationId from Person where intNationId is not nullselect @id=intParent from Basecode 
    where intParent<>0 and intId=@idselect @sql=@sql+',isnull(c.strCaption,''未知'') as ['+strCaption+']'
    from BaseCode
    where intId=@idexec('select a.intId as ID,a.strName as 姓名'+@sql+' from Person a left join BaseCode b on a.intEducationId=b.intId left join BaseCode c on a.intEducationId=c.intId')
      

  4.   

    还有点点问题,解答后马上给分,主要是分不多了,谢谢大家了
       姓名        学科           成绩
    ——————————————————
    张三 语文 85
    张三 英语 90
    张三 数学          95
    李四 语文 79
    李四 英语 87
    李四 数学 90
    王五 语文 80
    王五 英语 85
    王五 数学 70请问如何得到以下格式的查询结果
    姓名       语文      英语      数学
    ———————————————————
    张三       85        90         95
    李四       79        87         90
    王五       80        85         70我是这么写的,可是老是提示       列名 '姓名' 无效。declare @sql varchar(8000)select @sql='select 姓名'
    select @sql=@sql+','''+cast('学科' as varchar(10))+'''=sum(case when 学科='+cast('学科' as varchar(10))+' then 成绩 end) '
    from (select distinct 学科 from grade) a
    select @sql=@sql+' from grade 'group by 姓名exec(@sql)
      

  5.   

    select 姓名,case when 学科='语文' then 成绩  end as 语文,case when 学科='数学then 成绩  end as 数学,case when 学科='数学‘ then 成绩  end as 数学 from grade
      

  6.   

    select   Person.strName as 姓名,  (select strCaption  from basecode where   Person.intEducationId=basecode.intId) as 学历 ,  (select strCaption  from basecode where   Person.intNationId=basecode.intId) as 民族 from  Person basecode设计不够合理
      

  7.   

    declare @sql varchar(8000)select @sql='select 姓名'
    select @sql=@sql+','''+cast(学科 as varchar(10))+'''=sum(case when 学科='''+cast(学科 as varchar(10))+''' then 成绩 end) '
    from (select 'a' 学科 union select 'b' union select 'c' union select 'd') a--select distinct 学科 from grade
    select @sql=@sql+' from grade group by 姓名'
    exec(@sql)