1.有如下几个数据表(Mysql库),根据要求写出Sql。
——————————————————————————
学生表:(字段:编号、学生姓名、出生日期、性别)
t_student
| id | name | birthday | gender |
|---|---|---|---|
| 3 | 张天诚 | 1989-01-01 | 男 |
| 4 | 李偲偲 | 1990-12-21 | 女 |
| 1 | 庞中华 | 1989-01-01 | 男 |
| 2 | 席永社 | 1990-12-21 | 女 |
| 5 | 马小雨 | 1991-12-21 | 男 |
| 6 | 王思亮 | 1990-05-20 | 男 |
| 7 | 庞中华 | 1993-09-08 | 男 |
——————————————————————————
课程表:(字段:课程号、课程名称、教师编号)
t_course
| id | cou_name | tea_id |
|---|---|---|
| C01 | 语文 | T002 |
| C02 | 数学 | T003 |
| C03 | 英语 | T004 |
——————————————————————————
成绩表:(字段:学生编号、课程编号、分数)
t_score
| stu_id | cou_id | score |
|---|---|---|
| 1 | C01 | 80 |
| 2 | C02 | 90 |
| 3 | C03 | 99 |
| 4 | C02 | 60 |
| 5 | C03 | 80 |
| 6 | C01 | 80 |
| 7 | C02 | 50 |
| 8 | C03 | 80 |
| 1 | C02 | 67 |
| 3 | C01 | 88 |
| 6 | C03 | 66 |
| 7 | C03 | 59 |
| 3 | C02 | 46 |
——————————————————————————
教师表:(字段:教师编号、教师姓名)
t_teacher
| id | name |
|---|---|
| T001 | 李四 |
| T002 | 张三 |
| T003 | 王五 |
| T004 | 赵六 |
——————————————————————————
查找1990年出生的学生名单;
select id name from t_student where year(birthday) = 1990;
查询所有学生的学号、姓名、选课数、总成绩;
select a.id,a.name,count(b.cou_id) as 选课数,SUM(b.score) as 总成绩
from t_student as a left join t_score as b
on a.id = b.stu_id
group by a.id,a.name;
查询出每门课程的及格人数和不及格人数;
SELECT cou_id,
sum(case when score>=60 then 1
else 0
end) as 及格人数,
sum(case when score < 60 then 1
else 0
end) as 不及格人数
from t_score
group by cou_id;
查询没有学全所有课的学生的学号、姓名;
select id,name
from t_student
where id in(
select stu_id
from t_score
group by stu_id
having count(cou_id) < (select count(id) from t_course)
);