根据条件查询某人的科目成绩。
如下表:查询甲、乙语文、数学、英语的成绩
姓名,科目,成绩 
甲    语文  75 
  乙    数学  80 
  乙    英语  90 
  甲    数学  85 
  丙  化学  80 b表是科目表    2个字段  id      科目 
如  id      科目 
    1      语文 
    2      数学 
    3      英语 
    4      化学 
    5      物理 
根据条件先查b表的科目,再根据b的科目查成绩。a表中没有科目的成绩补0.
如显示结果。
甲   语文 75
甲   数学 85
甲   英语 0
乙   英语 90
乙   语文  0
乙   数学 85.
如何显示结果?                                                                                                                                                                 

解决方案 »

  1.   

    select name,b.object,nvl(result,0) from a right join b on a.object=b.object
    order by name,b.object;
      

  2.   

    sql语句执行出错。
    显示结果出现以下空格: 
    甲  语文 75 
    甲  数学 85 
        英语 0 
    乙  英语 90 
        语文  0 
    乙  数学 85. 
    请各位帮帮我很急!
      

  3.   

    select name,b.object,nvl(result,0) from a right join b on a.object=b.object
    order by name,b.object;
    这个语句出不来。
      

  4.   

    先产生列表,再找成绩select t.name, t.subject, nvl(t3.result,0) from (
    select t1,name, t2,subject 
    from 
      (select a.name from a where a.name ='甲' or a.name='乙') t1,
      (select b.subject from b where b.subject_id <=3) t2
    left join a
    on t2.name=a.name and t2.subject=a.subject
    order by t.name, t.subject;
      

  5.   

    在试试这个行不
    select c.name,c.object,nvl(result,0) 
    from a right join (select * from (select distinct name from a),b) c on a.object=c.object and a.name=c.name
    order by name,b.object;
      

  6.   

    select t.name, t.subject, nvl(t3.result,0) from (
    select t1,name, t2,subject 
    from 
      (select a.name from a where a.name ='甲' or a.name='乙') t1,
      (select b.subject from b where b.subject_id <=3) t2
    left join a
    on t2.name=a.name and t2.subject=a.subject) t
    order by t.name, t.subject;
      

  7.   


    select c.name,c.object,nvl(result,0) 
    from a right join (select * from (select distinct name from a),b) c on a.object=c.object and a.name=c.name
    order by name,c.object;
      

  8.   


    select Ta.name 姓名,Tb.subject 学科,nvl(Tc.grade,0) from (select distinct name from a) Ta,
                                   (select distinct subject from b) Tb,
                                   (select a.grade from a where a.name=Ta.name and b.subject = Tb.subject) Tc
      

  9.   

    用存储过程或在程序里组织吧
    一个sql挺难的
      

  10.   

    表结构
    test_table
    col1(名字)  col2(科目号)  col3(分数)test_table2
    aid(科目号)  cid(科目名)select w.col1 name,w.cid kemu ,nvl(t.col3,0) fenshu
      from
     test_table t,
     (
        select a.*,b.*
          from test_table a,
               test_table2 b
      ) w
      
      where w.aid=t.col2(+)
      and w.col1 = t.col1(+)
      
      group by w.col1,w.cid,t.col3
      order by w.col1,w.cid