sql之查询课程数及格的学生姓名

一、sql 问题

两张表,一张学生表,一张成绩表
查询:三门功课全部及格的学生。

学生表:user
id name
1 张三
2 李四
3 王五


成绩表:score

id user_id subject score
1 1 yuwen 30
2 1 shuxue 80
3 1 waiyu 70
4 2 yuwen 70
5 2 shuxue 80
6 2 waiyu 90
7 3 yuwen 50
8 3 shuxue 60
9 3 waiyu 70



二、答案

select 
    b.name 
from(
    select 
        a.name, 
        count(*) as num
    from(
        select 
            u.name, u.id, s.score
        from  
            score s
            left join user u
            on s.user_id = u.id
    ) a
    where 
        a.score between 60 and 100
    group by 
        a.name
) b
where
    b.num = 3;





三、操作过程


create table user(
    id bigint,
    name varchar(30)
);


insert into user(id, name) value (1, 'zhangsan');
insert into user(id, name) value (2, 'lisi');
insert into user(id, name) value (3, 'wangwu');


create table score(
    id bigint,
    user_id bigint,
    subject varchar(30),
    score int
);

insert into score (id,user_id,subject,score) values (1,1, 'yuwen', 30);
insert into score (id,user_id,subject,score) values (2,1, 'shuxue', 80);
insert into score (id,user_id,subject,score) values (3,1, 'yingyu', 70);

insert into score (id,user_id,subject,score) values (4,2, 'yuwen', 70);
insert into score (id,user_id,subject,score) values (5,2, 'shuxue', 80);
insert into score (id,user_id,subject,score) values (6,2, 'yingyu', 90);

insert into score (id,user_id,subject,score) values (7,3, 'yuwen', 50);
insert into score (id,user_id,subject,score) values (8,3, 'shuxue', 60);
insert into score (id,user_id,subject,score) values (9,3, 'yingyu', 70);




mysql> select 
       b.name 
   from(
       select 
           a.name, 
           count(*) as num
       from(
           select 
               u.name, u.id, s.score
           from  
               score s
               left join user u
               on s.user_id = u.id
       ) a
       where 
           a.score between 60 and 100
       group by 
           a.name
   ) b
   where
       b.num = 3;
+------+
| name |
+------+
| lisi      |
+------+
1 row in set (0.00 sec)



四、过程中遇到的问题记录



mysql> select 
           name 
       from(
           select 
               name, 
               count(*) as num
           from(
               select 
                   u.name, u.id, s.score
               from  
                   score s
                   left join user u
                   on s.user_id = u.id
           )
           where 
               score between 60 and 100
           group by 
               name
       )
       where
           num = 3;

ERROR 1248 (42000): Every derived table must have its own alias




五、拓展:查询成绩高于平均成绩的学生

SELECT name
FROM exam
WHERE score < ( SELECT AVG(score) FROM exam) 




六、拓展:sql 书写格式


SELECT   o.CustomerID, 
         SUM(UnitPrice * Quantity) AS TotalSales 
FROM     OrderDetails AS od 
         INNER JOIN Orders AS o ON od.OrderID = o.OrderID 
GROUP BY o.CustomerID





-
转载请注明,
原文出处:http://lixh1986.iteye.com/blog/2400406










-

猜你喜欢

转载自lixh1986.iteye.com/blog/2400406