Mysql联表查询(学生表、教师表、成绩表、课程表)

article/2025/11/10 11:39:44

文章目录

        • 1 建库语句
        • 2 建表语句
        • 3 插入测试数据
        • 4 常见查询问题

1 建库语句

create database if not exists studentinfo character set utf8mb4;

2 建表语句

在这里插入图片描述

一个学生可以选多门课程,一个课程下也对应多个学生,所以需要一个关系表(student_course)。为了方便,除了将学生id和课程id放入student_course中,还将成绩一起放到关系表中。另外一门课程对应一个老师。

(1) 学生表(student)

create table student(s_id int,sname varchar(20),sage int,sgender varchar(8)
)

(2) 课程表(course)

create table course(c_id int,cname varchar(20),t_id int
)

(3) 关系表(student_course)—— 学生和课程之间的关系表

create table student_course(s_id int, # 学生idc_id int, # 课程idscore int # 成绩
)

(4) 教师表(teacher)

create table teacher(t_id int,  #老师idtname varchar(20)  # 老师姓名
)

3 插入测试数据

(1)插入学生数据

insert into student select 1,N'刘一',18,N'男' union allselect 2,N'钱二',19,N'女' union allselect 3,N'张三',17,N'男' union allselect 4,N'李四',18,N'女' union allselect 5,N'王五',17,N'男' union allselect 6,N'赵六',19,N'女' ;

注意:

  • N表示后续的字符串是Unicode类型的字符。
  • Union:对两个结果集进行并集操作,不包括重复行,同时进行默认规则的排序;
  • Union All:对两个结果集进行并集操作,包括重复行,不进行排序;
  • 注意使用insert插入时可以使用select(需要保证查询出的字段和插入字段一致就可以了)
insert into student values(1,’张三’,1203);
insert into test_user(name, email) select name, qq_mail from student;

(2)插入教师数据

 insert into teacher select 1,N'叶平' union allselect 2,N'贺高' union allselect 3,N'杨艳' union allselect 4,N'周磊';

(3)插入课程数据

 insert into course select 1,N'语文',1 union allselect 2,N'数学',2 union allselect 3,N'英语',3 union allselect 4,N'物理',4;

(4)插入关系表数据

insert into student_course select 1,1,56 union all select 1,2,78 union all select 1,3,67 union all select 1,4,58 union all select 2,1,79 union all select 2,2,81 union all select 2,3,92 union all select 2,4,68 union all select 3,1,91 union all select 3,2,47 union all select 3,3,88 union all select 3,4,56 union all select 4,2,88 union all select 4,3,90 union all select 4,4,93 union all select 5,1,46 union all select 5,3,78 union all select 5,4,53 union all select 6,1,35 union all select 6,2,68 union all select 6,4,71;

4 常见查询问题

(1) 查询id为“001”的课程比“002”成绩高的所有学生的学号;

	 select a.s_id FROM(select s_id,score from student_course where c_id = '001') a,(select s_id,score from student_course where c_id = '002') bwhere a.score > b.score and a.s_id = b.s_id;
| s_id |
+------+
|    3 |

(2) 查询平均成绩大于60分的学号和平均成绩;

select s_id,avg(score)from student_coursegroup by s_idhaving avg(score)>60;
| s_id | avg(score) |
+------+------------+
|    1 |    64.7500 |
|    2 |    80.0000 |
|    3 |    70.5000 |
|    4 |    90.3333 |

(3) 查询所有学生的学号、姓名、选课数、总成绩;

select s.s_id,s.sname, count(sc.c_id),sum(sc.score) from 
student s left join student_course sc on s.s_id = sc.s_id 
group by s.s_id, s.sname;
| s_id | sname  | count(student_course.c_id) | sum(score) |
+------+--------+----------------------------+------------+
|    1 | 刘一   |                          4 |        259 |
|    2 | 钱二   |                          4 |        320 |
|    3 | 张三   |                          4 |        282 |
|    4 | 李四   |                          3 |        271 |
|    5 | 王五   |                          3 |        177 |
|    6 | 赵六   |                          3 |        174 |

(4) 查询姓“叶”的老师的个数;

select count(*) from teacher where tname like '叶%';
| count(*) |
+----------+
|        1 |

(5) 查询没学过“叶平”老师课的同学的学号、姓名;

 select s.s_id, s.sname from student s where s.s_id not in (select sc.s_id from student_course sc, course c, teacher t where sc.c_id = c.c_id and c.t_id=t.t_id and  t.tname='叶平');
| s_id | sname  |
+------+--------+
|    4 | 李四   |

(6) 查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;

select s.s_id, s.sname from student s,student_course sc where 
sc.s_id = s.s_id and sc.c_id = '001' 
and exists (select * from student s1, student_course sc1where s1.s_id=sc1.s_id and sc1.c_id='002' );
| s_id | sname  |
+------+--------+
|    1 | 刘一   |
|    2 | 钱二   |
|    3 | 张三   |
|    6 | 赵六

注:
exists对应的子查询实际上并不返回任何数据,而是返回值True或False,EXISTS 指定一个子查询,主要用来检测 行 的存在。

(7) 查询学过“叶平”老师所教的所有课的同学的学号、姓名;

 select s.s_id,s.sname from student s where s.s_id in (select sc.s_id from student_course sc,course c, teacher t where sc.c_id = c.c_id and c.t_id = t.t_id and t.tname ='叶平');
| s_id | sname  |
+------+--------+
|    1 | 刘一   |
|    2 | 钱二   |
|    3 | 张三   |
|    5 | 王五   |
|    6 | 赵六   |

(8) 查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名;

select a.s_id,a.sname from 
(select sc.s_id,sc.score,s.sname from student_course sc,student s 
where sc.s_id=s.s_id and sc.c_id='001')a, 
(select sc.s_id,sc.score from student_course sc,student s 
where sc.s_id=s.s_id and sc.c_id='002') b 
where a.s_id=b.s_id and a.score>b.score;
| s_id | sname  |
+------+--------+
|    3 | 张三   |
+------+--------+

(9) 查询所有课程成绩小于60分的同学的学号、姓名;

select s.s_id,s.sname from student s 
where s.s_id in (select sc.s_id from student_course sc 
where sc.score<60);
 s_id | sname  |
+------+--------+
|    1 | 刘一   |
|    3 | 张三   |
|    5 | 王五   |
|    6 | 赵六   |

(10) 查询没有学全所有课的同学的学号、姓名;

 select s.s_id,s.sname from student s, student_course sc where sc.s_id = s.s_id group by s.s_id,s.sname having count(sc.c_id)<(select count(c_id) from course);
| s_id | sname  |
+------+--------+
|    4 | 李四   |
|    5 | 王五   |
|    6 | 赵六   |

注意:
where子句中不能使用count等聚合函数,where 子句的作用是在分组之前过滤数据,having在分组之后过滤数据,条件中经常包含聚合函数。

(11) 查询至少有一门课与学号为“1”的同学所学相同的同学的学号和姓名;

select distinct s.s_id,s.sname from student s,student_course sc 
where s.s_id=sc.s_id and sc.c_id in 
(select c.c_id from student s, course c, student_course scwhere s.s_id = sc.s_id and s.s_id = '1');
| s_id | sname  |
+------+--------+
|    1 | 刘一   |
|    2 | 钱二   |
|    3 | 张三   |
|    4 | 李四   |
|    5 | 王五   |
|    6 | 赵六   |

(12) 对学生表的sname的最后两个个字母,按照升序进行排列

select sname from student order by substr(sname,length(sname)-1,2);

substr的用法:

substr(string string,num start,num length);string为字符串;start为起始位置;length为长度。

参考文章:http://www.manongjc.com/article/41690.html


http://chatgpt.dhexx.cn/article/UYkt42oW.shtml

相关文章

mysql实操《学生表》

第一步&#xff0c;创建所需要的的表信息 1.创建学生表和添加所需要的信息 Student(SID,Sname,Sage,Ssex) --SID 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别 添加测试数据1.学生表 insert into Student values(‘01’ , ‘赵雷’ , ‘1990-01-01’ , ‘男’); inser…

用Mysql设计一张学生表

mysql> create table student(-> id int,-> name varchar(10),-> gender char(1),-> birthday date,-> score double(5,2),-> email varchar(64),-> status tinyint#最后一行不用加逗号。-> ); Query OK, 0 rows affected (0.02 sec)mysql> desc…

Java数据库建立学生表

Java数据库建立学生表 代码如下&#x1f447;&#x1f447;&#x1f447;&#xff1a; import java.sql.*; //数据库建立学生表 public class Mysql {//数据库名为world&#xff0c;用户名为root&#xff0c;密码为adminprivate static String url "jdbc:mysql://localho…

mysql建立班级表_mysql数据表设计-班级表 学生表 老师表 课程表 成绩表

mysql数据表设计- 班级表 学生表 老师表 课程表 成绩表 image.png /* Navicat Premium Data Transfer Source Server : localhost Source Server Type : MySQL Source Server Version : 50624 Source Host : localhost Source Database : sqlexam Target Server Type : MySQL T…

mysql创建学生表命令_用sql语句创建学生表如何做

在数据库中使用SQL语句创建学生表代码如下&#xff1a;( 学号 char(12) primary key, 姓名 char(6) not null, 性别 char(2) check(性别 IN (男,女)), 班级 char(15) ) 相关表的创建&#xff1a;create table 成绩表 ( 学号 char(12)references 学生表(学号), 课程号 char(2)re…

SQL的学习学生表

1.创建数据库 create database school; 2.创建学生表&#xff0c;成绩表&#xff0c;课程表 create table student( Sno int primary key not null, Sname varchar(20) not null, Ssex varchar(2) null, Birthday datetime null, Sdept varchar(20) null);create table course(…

学生表

创建 1.学生表 Student(SID,Sname,Sage,Ssex) --SID 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别 2.课程表 Course(CID,Cname,TID) --CID --课程编号,Cname 课程名称,TID 教师编号 3.教师表 Teacher(TID,Tname) --TID 教师编号,Tname 教师姓名 4.成绩表 SC(SID,CID,s…

学生表 成绩表 课程表 教师表

学生表&#xff1a; Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别 课程表&#xff1a; Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号 教师表&#xff1a; Teacher(t_id,t_name) –教师编号,教师姓名 成绩表&#xff1a; Score(…

MySQL基础 创建学生表实现查询基础功能

一. 创建学生表 1.进入MySQL WIN R 输入cmd 输入mysql -u root -p 输入密码进入mysql 2. 输入show databases; 查询现有数据库 选择一个数据库 use 数据库名称 创建一个学生表 create table stu( id int ,-- 编号 name varchar(10),-- 姓名 gender char(1)…

数据库学生表

文章目录 学生表建立学生表&#xff1a;建表过程中出现的错误&#xff1a; 插入数据 课程表建立课程表&#xff1a;插入数据 选课表建立选课表插入数据建表过程中出现的错误&#xff1a; 修改基本表增加属性列&#xff1a;修改数据类型&#xff1a;增加约束条件&#xff1a;删除…

LDA理解

LDA 过程&#xff1a; 希拉里邮件lda建模&#xff1a;

LDA步骤

自己学习备忘用。节选自(1条消息) LDA原理小结_Sual-CSDN博客_lda原理

NLP: LDA主题模型

Essence本质&#xff1a;LDA模型主要包括主题分布θ和词语分布&#xff0c; 主题分布&#xff1a;各个主题在文档中出现的概率分布。词语分布&#xff1a;各个词语在某个主题下出现的概率分布。 pLSA模型中这两个分布是固定的&#xff0c;由期望最大化EM(Expectation Maximiz…

LDA算法

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、LDA是什么&#xff1f;二、公式推导三、PCA和LDA的区别总结 前言 线性判别分析&#xff08;LDA&#xff09;是一种有监督学习算法&#xff0c;同时经常被用…

自然语言处理7——LDA

文章目录 1. PLSA、共轭先验分布&#xff1b;LDA主题模型原理1.1 PLSA1.2 共轭先验分布1.2.1 共轭先验分布的参数确定1.2.2 常见的共轭先验分布 1.3 LDA主题模型原理 2. LDA应用场景3. LDA优缺点4. LDA 在sklearn中的参数学习5. 使用LDA生成主题特征&#xff0c;在之前特征的基…

matlab lda数据降维,LDA有标签数据降维

之前无标签数据降维PCA,那么像下图带有标签数据,如果用PCA降维将会投影到v轴上,这个投影方差最大,数据将变成不可分状态,LDA将把数据投影加上已有分类这个变量,将数据投影到u轴上 假设原数据分成n类,用矩阵Di表示i类数据,均值向量mi,将设将数据投影到向量w上后,均值分…

LDA模型,主题聚类模型

自然语言处理--LDA主题聚类模型 LDA模型算法简介&#xff1a; 算法 的输入是一个文档的集合D{d1, d2, d3, ... , dn}&#xff0c;同时还需要聚类的类别数量m&#xff1b;然后会算法会将每一篇文档 di 在 所有Topic上的一个概率值p&#xff1b;这样每篇文档都会得到一个概率的集…

LDA主题模型笔记

Table of Contents 1、写在前面 2、数学知识 3、文本建模 4.LDA主题模型实战 1、写在前面 在机器学习领域&#xff0c;关于LDA有两种含义&#xff0c;一是线性判别分析&#xff08;Linear Discriminant Analysis&#xff09;&#xff0c;是一种经典的降维学习方法&#xf…

LDA 主题模型

背景 我们生活中总是产生大量的文本&#xff0c;分析这些观察到的语料库是如何生成的就需要对文本进行建模。常见的文本建模方法包括&#xff1a;Unigram、PLSA、LDA 、词向量模型&#xff08;CBOW、Skip-gram&#xff09;等。LDA模型是一种主题模型&#xff08;topic model&a…

lda php,LDA主题分析代码实现

主题文本分析&#xff1a; 首先读取txt文本&#xff0c;并删除stop_list中存放的停止词汇 f open(..\\LDA_test.txt) texts [[word for word in line.strip().lower().split() if word not in stop_list] for line in f] print(Text ) pprint(texts)  #输出格式处理好的文本…