前言:本章咱们将学习SQL查询中的高级部分,如内链接、外链接和子查询,经过这些查询技术咱们将可以解决项目中复杂的查询问题。mysql
外键约束sql
MySQL属于关系型的数据库,表之间能够创建关系,如:学生表和成绩表,在成绩表中添加学生编号引用学生表中的学生编号,这样在成绩表中就不用添加剧复的学生信息了,这种关系也叫主外键关系,能够经过设置外键约束实现。数据库
能够在建立表时,添加外键约束来保证表和表之间引用完整性,添加外键后:学习
在插入外键表数据前,必须先插入主表数据spa
在删除主表数据前,必须先删除外键表数据blog
语法:rem
create table 表名table
(bfc
字段名 类型 约束,select
... ,
constraint 外键名称 foreign key (外键列) references 主表(主键)
);
代码示例:
use mysql_db;
-- 建立成绩表
drop table if exists tb_score;
create table tb_score
(
score_id int primary key auto_increment,
score_stu_id int,
score int,
score_course varchar(20),
constraint fk_score_stu_id foreign key(score_stu_id) references tb_student(stu_id)
);
内链接查询
在查询时咱们常常要把相关的多张表的字段,一块儿查询出来,如查询学生成绩时,要显示分数和学生姓名。这个时候咱们就须要链接查询了,链接查询分为内链接和外链接,咱们先学习内链接查询。
内链接查询的特色是:会查询出相关表中都存在的数据。
语法有两种实现方法:
1)select 字段..... from 表1 inner join 表2
on 表1.主键 = 表2.外键;
注意:这里假设表1是主表,内链接表的先后顺序无关
2)select 字段..... from 表1 , 表2
where 表1.主键 = 表2.外键;
代码示例:
-- 查询学生姓名和成绩 方式1
select s.stu_id ,s.stu_name,c.score_course,c.score from tb_score c inner join tb_student s on s.stu_id = c.score_stu_id;
-- 方式2
select s.stu_id ,s.stu_name,c.score_course,c.score from tb_score c , tb_student s where s.stu_id = c.score_stu_id;
效果相同:
外链接查询
外链接分为左外链接和右外链接:
1) 左外链接
链接查询多张表的数据,显示全部左表的数据,右表存在不相符的数据补null。
语法:
select 字段... from 左表 left join 右表
on 主表.主键 = 子表.外键;
代码示例:
-- 左外链接,查询学生姓名和成绩
select s.stu_id,s.stu_name,c.score_course,c.score from tb_student s left join tb_score c on s.stu_id = c.score_stu_id;
-- 查询全部参加过考试的同窗
select s.stu_id,s.stu_name,c.score_course,c.score from tb_student s left join tb_score c on s.stu_id = c.score_stu_id where c.score is not null;
-- 查询全部没参加过考试的同窗
select s.stu_id,s.stu_name,c.score_course,c.score from tb_student s left join tb_score c on s.stu_id = c.score_stu_id where c.score is null;
2)右外链接
与左链接相反,显示全部右表数据,左表中不相符的数据补null。
语法:
select 字段... from 左表 right join 右表
on 主表.主键 = 子表.外键;
代码示例:
select s.stu_id,s.stu_name,c.score_course,c.score from tb_score c right join tb_student s on s.stu_id = c.score_stu_id;
子查询
在查询语句中还能够嵌入查询语句,嵌入的查询也叫子查询,子查询将先执行,查询到结果后,父查询能够将此结果做为查询条件再进行一次查询,这样能够解决比较复杂的查询问题。
语法:
select ... from 表 where 字段 比较运算符 (select ... from 表 where 条件);
代码示例:
-- 查询年龄比李四大的学生
select * from tb_student where stu_age > (select stu_age from tb_student where stu_name = '李四');
-- 查询李四的老乡
select * from tb_student where stu_address = (select stu_address from tb_student where stu_name = '李四');
子查询之IN
有时候当子查询中查询结果不止一个的状况下,使用比较运算符会出现错误,这时候咱们就须要使用一些关键字来帮助筛选结果。
in关键字的做用是在字段和数据列表中任意一个相等,条件就成立。
代码示例:
-- 查询语文分数考相同的学生,先用子查询查语文的成绩,在用内链接查考过语文的学生姓名和成绩,把成绩进行比较
select stu_name,score_course,score from tb_student inner join tb_score on tb_student.stu_id = tb_score.score_stu_id where score_course='语文' and score in(select score from tb_score where score_course = '语文');
子查询之ALL
all和比较运算符配合使用,若是字段和全部的查询结果都比较成立,结果才成立。
语法:
字段 比较运算 all(查询结果)
代码示例:
-- 查询比全部男学生小的女学生,先查全部男学生的年龄,若是女学生年龄比全部这些年龄大,就查出来
select stu_name,stu_age,stu_gender from tb_student where stu_gender = '女' and stu_age < all(select stu_age from tb_student where stu_gender = '男');
子查询之ANY
any和比较运算符配合使用,若是字段和任意一个查询结果比较成立,则结果成立。
语法:
字段 比较运算 any(查询结果)
代码示例:
-- 查询只要比一个南京学生大的武汉学生
select stu_name,stu_address from tb_student where stu_address = '武汉'
and stu_age > any(select stu_age from tb_student where stu_address='南京');
子查询之Exists
exists表示是否有查询结果,若是没有结果,返回false,有结果则返回true
语法:
exists(查询结果)
-- 查询考过英语的同窗,在子查询中须要判断父查询中的学生id是否在子查询中存在
select * from tb_student where
exists(select score_stu_id from tb_score where tb_student.stu_id = tb_score.score_stu_id and score_course = '英语');
总结
本章咱们学习了内链接、外链接、子查询等高级查询方法,有时候这些查询方法须要综合运用起来,当咱们熟悉了它们后,查询数据就不是难事了。
本文由好程序整理上传。