MySQL学生表和分数表的多表查询

这篇文章提供了一系列关于SQL查询的示例,包括从学生表和分数表中提取信息,如查询所有记录、按条件筛选、分组统计、计算平均分等。这些练习涵盖了基础到进阶的SQL操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

         一、创建学生表

二、创建分数表

 1.查询student表的所有记录

2.查询student表的第2条到4条记录

3.从student表查询所有学生的学号(id)、姓名(name)和院系(department)的信息

4.从student表中查询计算机系和英语系的学生的信息

5.从student表中查询年龄30~35岁的学生信息

6.从student表中查询每个院系有多少人

7.从score表中查询每个科目的最高分

8.查询李四的考试科目(c_name)和考试成绩(grade)

9.用连接的方式查询所有学生的信息和考试信息

10.计算每个学生的总成绩

11.计算每个考试科目的平均成绩

12.查询计算机成绩低于95的学生信息

13.查询同时参加计算机和英语考试的学生的信息

14.将计算机考试成绩按从高到低进行排序

15.从student表和score表中查询出学生的学号,然后合并查询结果

16.查询姓张或者姓王的同学的姓名、院系和考试科目及成绩

17.查询都是湖南的学生的姓名、年龄、院系和考试科目及成绩


一、创建学生表

create table student (
    id int(10) not null unique primary key, 
    name  varchar(20) not null, 
    sex varchar (4), 
    brith year, 
    department varchar(20), 
    address varchar(50) 
);

二、创建分数表

create table score (
    -> id int(10) not null unique primary key auto_increment,
    -> stu_id int(10) not null,
    -> c_name varchar (20),
    -> grade int(10)
    -> );

查看表的结构是否建立

插入数据给两张表里面

向student表插入记录的INSERT语句如下:

INSERT INTO student VALUES( 901,'张老大', '男',1985,'计算机系', '北京市海淀区');
INSERT INTO student VALUES( 902,'张老二', '男',1986,'中文系', '北京市昌平区');
INSERT INTO student VALUES( 903,'张三', '女',1990,'中文系', '湖南省永州市');
INSERT INTO student VALUES( 904,'李四', '男',1990,'英语系', '辽宁省阜新市');
INSERT INTO student VALUES( 905,'王五', '女',1991,'英语系', '福建省厦门市');
INSERT INTO student VALUES( 906,'王六', '男',1988,'计算机系', '湖南省衡阳市');

向score表插入记录的INSERT语句如下:

INSERT INTO score VALUES(NULL,901, '计算机',98);
INSERT INTO score VALUES(NULL,901, '英语', 80);
INSERT INTO score VALUES(NULL,902, '计算机',65);
INSERT INTO score VALUES(NULL,902, '中文',88);
INSERT INTO score VALUES(NULL,903, '中文',95);
INSERT INTO score VALUES(NULL,904, '计算机',70);
INSERT INTO score VALUES(NULL,904, '英语',92);
INSERT INTO score VALUES(NULL,905, '英语',94);
INSERT INTO score VALUES(NULL,906, '计算机',90);
INSERT INTO score VALUES(NULL,906, '英语',85);

 1.查询student表的所有记录


2.查询student表的第2条到4条记录

SELECT * FROM student LIMIT 1,3;

3.从student表查询所有学生的学号(id)、姓名(name)和院系(department)的信息

MSELECT id,NAME,department FROM student;

4.从student表中查询计算机系和英语系的学生的信息

select *from student 
where department = '计算机系' or department = '英语系';

5.从student表中查询年龄30~35岁的学生信息

select * , year(now())-brith as age 
from student where brith < 1992 and brith > 1987;

6.从student表中查询每个院系有多少人

select department as '院系',count(department) as `院系人数` 
from student  group by department;

7.从score表中查询每个科目的最高分

select c_name as '科目', max(grade) as '最高分'
from score group by c_name;

8.查询李四的考试科目(c_name)和考试成绩(grade)

select s.id , s.name,s.sex,s.department,c.c_name 
from student as s inner join score as c 
on s.id=c.stu_id where s.name='李四';

9.用连接的方式查询所有学生的信息和考试信息

select s.id,s.name,s.sex,s.brith,s.department,s.address,
c.c_name,c.grade from score as c 
inner join student as s  
on s.id = c.stu_id ;

第二种方法:

SELECT st.id,st.name,st.sex,st.brith,st.department,st.address,
group_concat(s.c_name) as '科目',
group_concat(s.grade)as '成绩' 
FROM score  s 
LEFT JOIN student  st 
ON s.stu_id = st.id GROUP BY st.id;

10.计算每个学生的总成绩

SELECT st.name,sum(s.grade) as'总成绩' FROM score  s 
LEFT JOIN student  st ON s.stu_id = st.id GROUP BY st.id;

11.计算每个考试科目的平均成绩

SELSELECT c_name,avg(grade) as'平均成绩' FROM score  
GROUP BY c_name;

12.查询计算机成绩低于95的学生信息

select st.id,st.name,st.sex,st.brith,st.department,s.c_name,s.grade 
from score as s 
inner join student as st 
on st.id = s.stu_id 
where c_name='计算机'  and grade < 95 ;

13.查询同时参加计算机和英语考试的学生的信息

SELECT st.id,st.name,st.sex,st.brith,st.department,st.address,
group_concat(s.c_name) as '科目' FROM score  s 
LEFT JOIN student  st 
ON s.stu_id = st.id 
GROUP BY st.id 
having `科目`='计算机,英语';

14.将计算机考试成绩按从高到低进行排序

SELECT st.id,st.name,st.sex,st.brith,st.department,st.address,
s.c_name,s.grade 
FROM score s 
inner JOIN student st 
ON s.stu_id = st.id 
where s.c_name='计算机' 
order by grade desc;

15.从student表和score表中查询出学生的学号,然后合并查询结果

16.查询姓张或者姓王的同学的姓名、院系和考试科目及成绩

select st.name,st.department,s.c_name,s.grade 
from score as s 
inner join student as st 
on st.id=s.stu_id 
where st.name like '张%' or st.name like '王%';

第二种方法:

SELECT NAME,department,c_name,grade FROM score,
(SELECT * FROM student WHERE NAME LIKE '张%' OR NAME LIKE '王%')st 
WHERE stu_id=st.id;

17.查询都是湖南的学生的姓名、年龄、院系和考试科目及成绩

select name,(year(now())-brith) as age, department,c_name,grade 
from score ,
(select *from student where address like '湖南%')st 
where st.id=stu_id;

 第二种方法:

select name,(year(now())-brith) as age, department,c_name,grade 
from score as s 
inner join student as st 
on s.stu_id=st.id 
where address like '湖南%';

### MySQL联查:学生与班级的示例 在MySQL中,联查是一种常见的操作方式,用于从中提取所需的数据。以下是基于学生班级关系的一个具体示例。 #### 查询需求 假设有一个场景:我们需要查询出一班成绩高于二班最低成绩学生信息。这可以通过`LEFT JOIN`以及子查询的方式实现。 #### SQL语句示例 ```sql SELECT DISTINCT student.* FROM sc LEFT JOIN student ON sc.sid = student.sid WHERE student.classid = 1 AND score > ANY ( SELECT score FROM sc LEFT JOIN student ON sc.sid = student.sid WHERE student.classid = 2 ); ``` 上述SQL语句的功能如下: - `sc` 存储学生的课程分数记录。 - `student` 存储学生的基本信息及其所属班级。 - 使用 `LEFT JOIN` 将两个连接起来,以确保即使某些学生成绩不存在也能返回其基本信息[^5]。 - 条件部分筛选了一班 (`classid=1`) 的学生,并且这些学生成绩要大于二班 (`classid=2`) 中任意一个学生的最低分[^2]。 #### 自关联查询的应用 如果需要在一个内部进行比较或者查找父子关系等复杂逻辑时,可以采用自关联查询的方法。例如,在同一个中找到某个字段与其他行的关系: ```sql SELECT t1.name, t1.parent_id, t2.name AS parent_name FROM tree_table t1 LEFT JOIN tree_table t2 ON t1.parent_id = t2.id; ``` 这里的关键在于为同一张赋予不同的别名(如`t1`, `t2`),从而能够区分不同角色下的相同数据结构[^3]。 #### 子查询依赖外部上下文的情况 需要注意的是,当子查询涉及到外层查询中的列时,必须保证该子查询是在整体查询框架下运行的。比如下面这个错误的例子会引发异常:“Unknown column 'o.cno' in 'where clause'”,因为单独执行子查询无法识别来自外部查询的变量`o.cno`[^4]。 正确写法应保持子查询嵌套于主查询之中,而不是独立出来测试。 --- ### 总结 以上展示了如何利用MySQL完成针对学生班级之间复杂的联合查询任务,包括但不限于跨条件过滤、使用集合函数(`ANY`)处理不平等匹配等问题解决思路。同时强调了自关联技术的重要性及注意事项。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值