Sql语句:
create table student_one(
sno varchar(20) not null primary key,
sname varchar(20) not null,
ssex varchar(20) not null,
sbirthday datetime,
class varchar(20)
)charset=utf8;
加入数据:
insert into student_one values('1','陈奇','男','2000-11-02','1');
insert into student_one values('2','陈飞','男','1999-12-05','2');
insert into student_one values('3','周杰','女','1989-03-01','3');
insert into student_one values('4','昆凌','男','1989-04-15','4');
insert into student_one values('5','王丽','女','1999-05-16','5');
insert into student_one values('6','蔡蔡','男','2000-08-17','6');
Sql语句:
create table teacher_one
(
tno varchar(20) not null primary key,
tname varchar(20) not null,
tsex varchar(20) not null,
depart varchar(20) not null
)charset=utf8;
加入数据:
insert into teacher_one values('100','斯内普','男','计算机');
insert into teacher_one values('101','哈利波特','男','土木工程');
insert into teacher_one values('102','德拉科','男','中文');
insert into teacher_one values('103','郝敏','女','历史');
insert into teacher_one values('104','莉莉','女','舞蹈');
Sql语句:
create table course_one(
cno varchar(20) not null primary key,
cname varchar(20) not null,
tno varchar(20) not null,
foreign key(tno) references teacher_one(tno)
)charset=utf8;
加入数据:
insert into course_one values('0-1','计算机导论','100');
insert into course_one values('1-5','混凝土结构','101');
insert into course_one values('2-1','古代文论','102');
insert into course_one values('3-2','世界古代史','103');
insert into course_one values('4-2','蒙古舞','104');
Sql语句:
create table score_one(
sno varchar(20) not null,
foreign key(sno) references student_one(sno),
cno varchar(20) not null,
foreign key(cno) references course_one(cno),
degree decimal
)charset=utf8;
加入数据:
insert into score_one values('1','0-1','99');
insert into score_one values('2','1-5','88');
insert into score_one values('3','2-1','77');
insert into score_one values('4','3-2','66');
insert into score_one values('5','4-2','98');
insert into score_one values('6','3-2','99');
多表查询的连接谓词
Join:将两个表连接在一起,可进行笛卡尔积、内连接、外连接
举例:
目的:将teacher_one表和course_one表做笛卡尔积
Sql语句:
SELECT *
FROM teacher_one
CROSS JOIN course_one;
内连接是最常见的一种连接,它也被称为普通连接和自然连接,但是只连接匹配的行,所以内连接可能丢失信息
语法
SELECT 查询的字段
FROM table1 JOIN table2
ON table_b ON table_a.外键 = table_b.主键;
举例:
目的:将学生学号,名字,成绩查询出来
Sql语句:
SELECT student_one.sno, student_one.sname, score_one.degree
FROM score_one JOIN student_one
ON score_one.sno = student_one.sno;
语法:
SELECT 查询的字段
FROM table_a , table_b
WHERE table_a.外键 = table_b.主键;
举例:
目的:将学生学号,名字,成绩查询出来
Sql语句:
SELECT student_one.sno, student_one.sname, score_one.degree
FROM score_one, student_one
WHERE score_one.sno = student_one.sno;
外连接查询扩充了内连接查询的功能,包含全外连接、左外连接、右外连接
保留第一个表所有行,而第二个表只包含与第一个表匹配的行,第二个表相应的空行为NULL值
语法:
SELECT 查询的字段
FROM table1 LEFT JOIN table2
ON 条件
举例:
目的:将每位老师的所教的课程查询出来
Sql语句:
SELECT teacher_one.tname,course_one.cname
FROM teacher_one LEFT JOIN course_one
ON course_one.tno = teacher_one.tno
保留第二个表所有行,而第一个表只包含与第二个表匹配的行,第一个表相应的空行为NULL值,与左外连接相反
语法
SELECT 查询的字段
FROM table1 RIGHT JOIN table2
ON 条件
举例:
目的:将学生的学号,姓名,成绩查询出来
Sql语句:
SELECT student_one.sno,student_one.sname,score_one.degree
FROM student_one RIGHT JOIN score_one
ON score_one.sno=student_one.sno
一条select语句结果作为另一条select语句的(查询条件,查询结果等)。
语法:
SELECT 查询的字段
FROM table_a
WHERE 字段 = (SELECT 字段 FROM table_a [WHERE 条件])
举例:
目的:将sno小于3的同学的成绩查询出来
Sql语句:
SELECT student_one.sname, score_one.degree
FROM student_one LEFT JOIN score_one
ON score_one.sno = student_one.sno
WHERE student_one.sno IN (SELECT sno FROM student_one WHERE student_one.sno <3)
注意:当子查询产生一系列的值时,用IN,如果只有单个的值,就用=
运行结果:
EXISTS:
EXISTS搜索条件并不真正使用子查询的结果,它仅仅测试子查询是否产生任何结果
语法:
SELECT 查询的字段
FROM table
WHERE EXISTS (条件)
举例:
目的:查询teacher_one表里面是否存在教师编号小于105的教师,不存在返回NULL
Sql语句:
SELECT teacher_one.tname, course_one.cname
FROM teacher_one LEFT JOIN course_one
ON course_one.tno = teacher_one.tno
WHERE EXISTS (SELECT tno FROM teacher_one WHERE teacher_one.tno <105)
NOT EXISTS:
与 EXISTS的作用相反,如果子查询没有返回行,则满足 NOT EXISTS的WHERE子句
语法:
SELECT 查询的字段
FROM table
WHERE NOT EXISTS (条件)
举例:
目的:查询teacher_one表里面是否存在教师编号等于109的教师,存在返回NULL,不存在返回数据
Sql语句:
SELECT *
FROM teacher_one
WHERE NOT EXISTS (SELECT tno FROM teacher_one WHERE tno=109)
语法:
查询语句A
UNION
查询语句B
举例:
目的:将性别为男,学号为5的同学的学号,姓名,年龄都查询出来
Sql语句:
SELECT sno,sname,YEAR(from_days(datediff(now(), sbirthday))) AS '年龄'
FROM student_one
WHERE ssex='男'
UNION ALL
SELECT sno,sname,YEAR(from_days(datediff(now(), sbirthday))) AS '年龄'
FROM student_one
WHERE sno=5