【MySQL数据库设计与应用(三)】数据查询(一)

1 Select 简单查询

查询概述

数据查询也称为数据检索,是从数据库中获取所需数据的操作和过程。用户根据实际应用的需求,可以使用不同的查询方式,以获取不同的数据。数据查询是数据库管理系统(DBMS)的重要功能之一,是数据库操作中最常见,最重要的操作。

基本查询语句

select 语句的基本语法:

SELECT select_expr [, select_expr……]	// 要查询的列(字段)或表达式
FROM table_references					// 指定查询的数据来源,即数据表
WHERE where_condition					// 查询时需要满足条件
GROUP BY col_name						// 对结果进行分组
HAVING where_condition					// 对分组内的数据进行筛选
ORDER BY col_name						// 查询结果进行排序
LIMIT row_count							// 限定输出的查询结果

单表查询
单表查是指从一张表的数据中查询所需数据。

查询所有字段:使用 * 匹配或列出表的所有字段
查指定字段:列出要查询的字段
定义查询结果中列的别名:AS 关键字

  • SELECT 字段名 [AS] 别名 FROM

替换查询结果中的数据:CASE…END

  • 如果想把查询结果的列值替换为另外的值,可以通过 CSAE……END 语句实现。

example:

select * from department;

select rtype, rprice
from  tb_room;

select
rtype as 房间,
rprice as 价格
from tb_room;
 
select
rtype AS 房间,
(case when rprice >= 500 then '高价' else '合理' end) as 价格
from tb_room;

select
rtype AS 房间,
(case when rprice >= 500 then '高价' else (case when rprice >= 300 then '合理' else '低价' end) end) as 价格
from tb_room;

2 条件查询

条件查询及 WHERE 条件判断符
带 IN 关键字的条件查询
带 BETWEEN AND 的范围查询
带 LIKE 关键字的字符匹配查询
空值查询(IS NULL)

2.1 条件查询及 WHERE 条件判断符

条件查询用于过滤数据,即查询结果中只包含满足条件的记录。在 WHERE 子句中多用关系运算符和逻辑运算符构造查询条件。

  • SELECT ……
    FROM table
    WHERE conditions;

example:

select * from tb_room where rprice=280;

select
student_id as 学号,
student_name as 姓名,
birthday as 生日
from student1
where birthday >= '1995-01-01' and birthday <= '1995-12-31';

2.2 带 IN 关键字的条件查询

IN 构造查询条件时,将条件用括号括起来,条件之间用逗号分隔开,只要满足其中一个条件即为满足条件。格式为:

  • WHERE fieldname IN ( 值1, 值2, …)

example:

select
student_id as 学号,
student_name as 姓名,
birthday as 出生年月
from student1
where year(birthday)  in (1994, 1995);

2.3 带 BETWEEN AND 的范围查询

BETWEEN AND 用来查询某个范围内的值,该操作符需要两个参数,即范围的开始值和结束值,如果字段值位于指定的范围之内,则这些记录被返回。格式为:

  • WHERE fieldname BETWEEN 开始值 AND 结束值

example:

select * from student1
where birthday between '1995-01-01' and '1995-12-31';

2.4 带 LIKE 关键字的字符匹配查询

如果只想按字段值的部分内容进行匹配,即执行“模糊查询” , 则需要使用关键字 LIKE 配合通配符 “%” 和 “_” 。

  • WHERE fieldname LIKE ‘匹配符和其他字符组合’

%:匹配任意长度的字符 。
_: 只能匹配任意一个字符 , 如果要匹配多个字符,则需要使用相同数量的 “_” 。

example:

select * from student1
where phone like '176%';

2.5 空值查询(IS NULL)

空值(NULL)不同于 0 , 也不同于空字符串。空值表示数据未知、不确定或将在以后添加数据。查询时使用 IS NULL 查询某字段值为空的记录 , 与之相反的字句为 IS NOT NULL

example:

select * from course
where begin_choose_time is null;

3 连接查询

连接是关系数据模型的主要特点,连接查询是关系数据库中最主要的查询,包括内连接、外连接等。

当两个或多个表中存在相同意义的字段时,便可以通过这些字段对不同的表进行连接查询,得到存放在多个表中的记录数据 。所谓表中相同意义的字段,是指在多个表中名字不一定相同,但取值的含义相同的字段,这是表之间实现连接查询的前提。

如:department.Department_id <==> teacher.Department_id

3.1 内连接查询 (INNER JOIN)

INNER JOIN 使用比较运算符(=)根据每个表共有列的列值匹配两个表中的行,其查询结果仅包含符合查询条件和连接条件的行。即查询结果为两个表的交集 。

注意: 多表查询时,为避免混淆,在查询字段前应添加表名称作为前缀!

  • SELECT tableA.select_list, tableB.select_list
    FROM tableA INNER JOIN tableB
    ON tableA.Key = tableB.Key

在这里插入图片描述

teacher 表和 department 表通过共有列 Department_id 实现连接 , 通过内连接查询 (INNER JOIN) 可以得到两个表中 Department_id 值相同的所有行。

SELECT
teacher.*,
department.*
FROM teacher INNER JOIN department
ON teacher.Department_id = department.Department_id;

example:

  • 学生表 student 和班级表 classes 的内连接查询
    SELECT … FROM student
    INNER JOIN classes ON student .Class_id = classes .Class_id;

  • 选课表 choose 和课程表 course
    SELECT … FROM choose
    INNER JOIN course ON choose .Course_id = course .Course_id;

  • student A、choose B、 course C 三个表的内连接查询
    SELECT
    A.Student_id, A.Student_name, C.Course_name, C.Term, B.Score
    FROM student A
    INNER JOIN choose B ON A.Student_id = B.Student_id
    INNER JOIN course C ON B.Course_id = C.Course_id;

在设计内连接查询时 , 强调几点:

  1. 表之间一定要有连接字段。
  2. 查询结果的多个列来源于不同的表,需要在列名称前加表名称作为前缀。
  3. 为了简化书写,可以给表指定别名,格式为:
    SELECT … FROM table A
    别名 A 和表名 table 之间至少加一个空格

example:

select
teacher.Teacher_id,
teacher.Teacher_Name,
teacher.Department_id,
department.Department_id,
department.Department_name
from teacher inner join department 
on teacher.Department_id=department.Department_id;

可以简化为下一种写法,给表起别名

select
A.Teacher_id,
A.Teacher_Name,
A.Department_id,
B.Department_id,
B.Department_name
from teacher A inner join department B
on A.Department_id = B.Department_id;

3.2 外连接查询 (OUTER JOIN)

内连接的查询结果仅包含符合连接条件的行。如果需要查询结果不仅包含符合连接条件的行, 而且还包括左表、右表或两个连接表中的所有数据行,则应该使用外连接 。

MySQL 支持的外连接有两种类型:

  • 左外连接(左连接 LEFT [OUTER] JOIN)
  • 右外连接( 右连接 RIGHT [OUTER] JOIN )

MySQL 不能直接支持 FULL JOIN ,要实现 FULL JOIN,应该使用 LEFT JOIN UNION RIGHT JOIN 的方式。

左连接(LEFT OUTER JOIN)
左连接的查询结果为左表的所有记录以及右表中连接字段相等的记录。如果左表的某行在右表中没有匹配行, 则在相关联的结果行中,右表的所有选择列均为空值 (NULL)。

  • SELECT tableA.select_list, tableB.select_list
    FROM tableA
    LEFT [OUTER] JOIN tableB
    ON tableA .Key = tableB .Key

在这里插入图片描述

右连接(RIGHT OUTER JOIN)
右连接是左连接的反向连接,查询结果为右表中的所有记录以及左表中连接字段相等的记录。如果右表的某行在左表中没有匹配行,左表的选择列将返回空值 (NULL)。

  • SELECT tableA.select_list, tableB.select_list
    FROM tableA
    RIGHT [OUTER] JOIN tableB
    ON tableA .Key = tableB .Key

在这里插入图片描述

完全连接返回左表和右表中的所有记录,包括连接字段相等的记录和不相等的记录。如果左表的某行在右表中没有匹配行,将返回空值 (NULL),反之亦然。

  • SELECT tableA.select_list, tableB.select_list
    FROM tableA LEFT [OUTER] JOIN tableB
    ON tableA .Key = tableB .Key
    UNION
    SELECT tableA.select_list, tableB.select_list
    FROM tableA RIGHT [OUTER] JOIN tableB
    ON tableA .Key = tableB .Key

在这里插入图片描述
example:

select
department.Department_id,
department.Department_name,
teacher.Teacher_id,
teacher.Teacher_Name
from department left join teacher
on department.Department_id = teacher.Department_id;

3.3 复合连接条件查询 ( 连接查询+WHERE)

复合条件连接查询是在连接查询的过程中,通过添加过滤条件,限制查询的结果,也就是 JOIN 和 WHERE 条件组合,使查询的结果更加准确。

# 查询第二学期学生的学号,姓名,课程与升级
select
student.student_id,
student.student_name,
course.Course_name,
course.term,
choose.score
from student
inner join choose on student.student_id = choose.Student_id
inner join course on choose.Course_id = course.Course_id
where course.term like '第二学期';

select
department.Department_id,
department.Department_name,
teacher.Teacher_id,
teacher.Teacher_Name
from department left join teacher
on teacher.Department_id = department.Department_id
where teacher.Teacher_id is null;
原创文章 303 获赞 189 访问量 11万+

猜你喜欢

转载自blog.csdn.net/happyjacob/article/details/105831497