多语言展示
当前在线:1823今日阅读:27今日分享:41

好程序员分享SQL语言之查询

本章我们将学习SQL查询中的高级部分,如内连接、外连接和子查询,通过这些查询技术我们将能够解决项目中复杂的查询问题。
方法/步骤
1

外键约束MySQL属于关系型的数据库,表之间可以建立关系,如:学生表和成绩表,在成绩表中添加学生编号引用学生表中的学生编号,这样在成绩表中就不用添加重复的学生信息了,这种关系也叫主外键关系,可以通过设置外键约束实现。可以在创建表时,添加外键约束来保证表和表之间引用完整性,添加外键后:在插入外键表数据前,必须先插入主表数据在删除主表数据前,必须先删除外键表数据语法:create table 表名(字段名 类型 约束,... ,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));

2

内连接查询在查询时我们经常要把相关的多张表的字段,一起查询出来,如查询学生成绩时,要显示分数和学生姓名。这个时候我们就需要连接查询了,连接查询分为内连接和外连接,我们先学习内连接查询。内连接查询的特点是:会查询出相关表中都存在的数据。语法有两种实现方法:1)select 字段..... from 表1 inner join 表2on 表1.主键 = 表2.外键;注意:这里假设表1是主表,内连接表的前后顺序无关2)select 字段..... from 表1 , 表2where 表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;效果相同:

3

外连接查询外连接分为左外连接和右外连接: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;

4

-- 查询所有参加过考试的同学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;

5

-- 查询所有没参加过考试的同学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;

7

子查询在查询语句中还可以嵌入查询语句,嵌入的查询也叫子查询,子查询将先执行,查询到结果后,父查询可以将此结果作为查询条件再进行一次查询,这样可以解决比较复杂的查询问题。语法:select ... from 表 where 字段 比较运算符 (select ... from 表 where 条件); 代码示例:-- 查询年龄比李四大的学生select * from tb_student where stu_age > (select stu_age from tb_student where stu_name = '李四');

8

-- 查询李四的老乡select * from tb_student where stu_address = (select stu_address from tb_student where stu_name = '李四');

9

子查询之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 = '语文');

10

子查询之ALLall和比较运算符配合使用,如果字段和所有的查询结果都比较成立,结果才成立。语法:字段 比较运算  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 = '男');

11

子查询之ANYany和比较运算符配合使用,如果字段和任意一个查询结果比较成立,则结果成立。语法:字段 比较运算  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='南京');

12

子查询之Existsexists表示是否有查询结果,如果没有结果,返回false,有结果则返回true语法:exists(查询结果)-- 查询考过英语的同学,在子查询中需要判断父查询中的学生id是否在子查询中存在select * from tb_student whereexists(select score_stu_id from tb_score where tb_student.stu_id = tb_score.score_stu_id and score_course = '英语');

注意事项

总结 本章我们学习了内连接、外连接、子查询等高级查询方法,有时候这些查询方法需要综合运用起来,当我们熟悉了它们后,查询数据就不是难事了。

推荐信息