学生表 s(id,name,sex)  课程表format代表课程名称   c(id,format)
成绩表cs(s_id,c_id,grade)要求 写一个sql语句  列出 女生的姓名及她的所有课程成绩和总成绩 条件是她至少有一门课程成绩>=60 
我写了一个 面试官说不对 大家来看看哈  50分

解决方案 »

  1.   

    你把你写的sql贴出来让大家看看不就知道了,面试管说的不算,运行后结果才能说明问题的
      

  2.   

    SELECT s.name,s.id, c.name, cs_t.grade
    FROM s, c,
    (SELECT cs.s_id, cs.c_id, cs.grade 
    FROM cs 
    (SELECT DISTINCT(cs.s_id) AS s_id
    FROM cs, s 
    WHERE cs.grade >= 60
    AND cs.s_id = s.id 
    AND s.sex = '女') t
    WHERE t.s_id = cs.s_id) cs_t
    WHERE s.id = cs_t.s_id
    AND c.id = cs_t.c_id 
    UNION ALL
    SELECT s.name,s.id, '总成绩', sum_t.grade
    FROM s, (
    SELECT cs.s_id as s_id, SUM(cs.grade) AS  grade
    FROM cs 
    (SELECT DISTINCT(cs.s_id) AS s_id
    FROM cs, s 
    WHERE cs.grade >= 60
    AND cs.s_id = s.id 
    AND s.sex = '女') t
    WHERE t.s_id = cs.s_id 
    GROUP BY cs.s_id) sum_t
      

  3.   

    貌似发错版了吧select s.name,cs.grade from s join cs on s.id=cs.s_id
    where s.sex='女' and s.id in (select cs.s_id from cs where cs.grade>=60)
    union
    select s.name,sum(cs.grade) from s join cs on s.id=cs.s_id
    where s.sex='女' and s.id in (select cs.s_id from cs where cs.grade>=60)
    group by s.id,s.name
      

  4.   

    根据你这题目,我也写了个SQL,我本来是想把所有的课程转换到COLUMN,可是不好意思,这个课程不是固定的,我没法将它进行行列转换(我的知识有限,希望高手可以指点),所以用了另一种方式,也可以满足题目的要求,如下:select aa.name,bb.format,cc.grade,sum(cc.grade) over (partition by aa.id) as "total" 
    from s aa,c bb,sc cc
    where aa.id = cc.s_id
    and bb.id = cc.c_id
    and exists (select 0 from sc where cc.s_id = sc.s_id and sc.grade > 60)
    and aa.sex = 'female'Thanks
    Hima