显示出现了问题!不好意思!!!重帖下:tblKaoshi 考试表 表中的内容如下:
ID TestDateTime YuWenCJ shuxueCJ
1 2005-4-25 00.00.00           89                98
2 2005-4-15 00.00.00           87                88
3 2005-6-23 00.00.00           86                95帮写些SQL代码得出一个表,结果如下:NAME 项目 TestDateTime 成绩
小强 语文 2005-4-25   89
小王 语文 2005-4-15   87
小高 语文 2005-6-23   86
小强 数学 2005-4-25   98
小王 数学 2005-4-15   88
小高 数学 2005-6-23   95

解决方案 »

  1.   

    select a.name b.* from tblxuesheng a,
    (
    select id,testdatetime,yuwencj  as chengji from tblkaoshi
    unill all
    select id,testdatetime,shuxuecj as chengji from tblkaoshi) b
    where a.id=b.id
      

  2.   

    create  table a

    NAME  char(5)  constraint  pk_NAME primary key  nonclustred(NAME),
    项目  char(5),  
    TestDateTime    datetime
    成绩  char(3)    constraint ck_成绩 check (成绩 >= '0 'and 成绩 <='100')
    )inster a (NAME 项目 TestDateTime 成绩)
    values('小强','语文','2005-4-25','89')
    以此类推向表中加入数据
    OK?
      

  3.   

    --测试数据
    create table #tblxuesheng(ID INT,NAME VARCHAR(10))
    INSERT #tblxuesheng select 1,'小强'
    INSERT #tblxuesheng select 2,'小王'
    INSERT #tblxuesheng select 3,'小高'create table #tblKaoshi(ID int,TestDateTime datetime,YuWenCJ int,shuxueCJ int)
    insert #tblKaoshi select 1,'2005-4-25',89,98
    insert #tblKaoshi select 2,'2005-4-15',87,88
    insert #tblKaoshi select 3,'2005-6-23',86,95
    select a.NAME,b.项目,b.TestDateTime,b.成绩 from #tblxuesheng a,
    (
    select ID,TestDateTime,'数学' 项目,YuWenCJ 成绩 from #tblKaoshi
    union 
    select ID,TestDateTime,'语文' 项目,shuxueCJ 成绩 from #tblKaoshi) b
    where a.ID=b.ID order by 项目 --结果
    NAME   项目    TestDateTime               成绩    
    -----  ----- ---------------------------- ------
    小强    数学   2005-04-25 00:00:00.000     89
    小王    数学   2005-04-15 00:00:00.000     87
    小高    数学   2005-06-23 00:00:00.000     86
    小强    语文   2005-04-25 00:00:00.000     98
    小王    语文   2005-04-15 00:00:00.000     88
    小高    语文   2005-06-23 00:00:00.000     95(所影响的行数为 6 行)