数据库连表查询
学生表student
老师表teacher
课程表course
选课表student_course
1 按“0001”号课程成绩由高到低顺序显示所有学生学号、姓名、成绩(二表连接);
SELECT
student.student_id,
student.student_name,
student_course.score
FROM
student,
student_course
WHERE
student.student_id = student_course.student_id
AND student_course.course_id = '0001'
ORDER BY
student_course.score DESC; #DESC代表降序排列;ASC代表升序排列
当两个(多个)表中有相同的字段(属性,列名)时,使用表名(或表别名).列名的形式对查询内容和条件内容进行区分。
运行结果:
2 按“高等数学”课程成绩由高到低顺序显示所有学生姓名、学号、分数、课程名(三表连接);
SELECT
st.student_id,
st.student_name,
sc.score,
co.course_name
FROM
student AS st,
student_course AS sc,
course AS co
WHERE
st.student_id = sc.student_id
AND sc.course_id = co.course_id
AND co.course_name = '高等数学'
ORDER BY
sc.score DESC; #DESC代表降序排列;ASC代表升序排列
运行结果:
3 查询有薪水差额的任意两位教师(单表连接查询);
SELECT
t1.teacher_id AS '教工号1',
t1.teacher_name AS '教师姓名1',
t2.teacher_id AS '教工号2',
t2.teacher_name AS '教师姓名2'
FROM
teacher t1,
teacher t2
WHERE
t1.teacher_salary > t2.teacher_salary;
运行结果:
4 查询既学过“0001”又学过“0002”号课程的所有学生学号;
SELECT
sc1.student_id '学号'
FROM
student_course sc1,
student_course sc2
WHERE
sc1.student_id = sc2.student_id
AND sc1.course_id = '0001'
AND sc2.course_id = '0002';
运行结果:
5 查询“0001”号课程比“0002”号课程成绩低的所有学生学号姓名;
SELECT
st.student_id '学号',
st.student_name '姓名'
FROM
student_course sc1,
student_course sc2,
student st
WHERE
sc1.student_id = sc2.student_id
AND sc1.student_id = st.student_id
AND sc1.course_id = '0001'
AND sc2.course_id = '0002'
AND sc1.score < sc2.score;
运行结果:
6. 查询“0003”老师教的所有的学生
SELECT
st.*
FROM
student as st inner join teacher as th on st.teacher_id=th.teacher_id where th.teacher_id =0003
mysql数据库分页查询
select * from student limit 2,3
运行结果
上面查询中的2代表从第2条数据之后开始,注意是第2条数据之后,不包含第2条数据。3代表总共查询3条记录。那么limit 2,3代表从第2条(不包含第2条) 数据开始查询出3条记录。
创建表及插入数据
我们按照以下的语句对我们的数据库操进行建表和插入数据,方便接下来对我们的两张表进行操作和演示
#数据库建表
CREATE TABLE IF NOT EXISTS 'student' (
'id' INT AUTO_INCREMENT,
'name' VARCHAR(18) NOT NULL COMMENT '姓名',
'age' VARCHAR(18) NOT NULL COMMENT '年龄',
'weight' VARCHAR(18) NOT NULL COMMENT '体重',
'sex' VARCHAR(18) NOT NULL COMMENT '性别',
'height' VARCHAR(18) NOT NULL COMMENT '身高',
'teacher_id' INT NOT NULL COMMENT '教师id',
'create_time' datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
PRIMARY KEY ('id')
) ENGINE = InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS 'teacher'(
'id' INT AUTO_INCREMENT,
'name' VARCHAR(18) NOT NULL COMMENT '姓名',
'age' VARCHAR(18) NOT NULL COMMENT '年龄',
'weight' VARCHAR(18) NOT NULL COMMENT '体重',
'height' VARCHAR(18) NOT NULL COMMENT '身高',
'sex' VARCHAR(18) NOT NULL COMMENT '性别',
'create_time' datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
PRIMARY KEY ('id')
)ENGINE = InnoDB DEFAULT CHARSET=utf8;
# 在学生表中插入数据
INSERT INTO student(name,age,weight,sex,height,teacher_id)VALUES
("花花","18","50","女","165",1),
("菲菲","17","47","男","162",1),
("西西","19","45","女","161",2),
("玥玥","16","40","女","160",1),
("涵涵","15","49","男","164",2),
("涵涵","17","48","男","163",2),
("若若","20","45","女","161",1);
# 在老师表中插入数据
INSERT INTO teacher (name,age,weight,height,sex)VALUES
("高飞","26","64","170","女"),
("张宇","28","74","179","男");
查看学生表有无数据
查看老师表有无数据
删除表
更新表
排序
这里时间是一样的,因为我是批量插入的所以没有变化
分组
这里的涵涵原本是有两个的现在只有一个了
如果执行上面这句报错进入/etc目录下,在my.cnf中加上下面图片所示的代码
连表查询
查询多少个女学生
查询男生的总体重
增加一个老师表中的字段,用于统计有多少学生
更新老师表中的数据