0 准备工作
进行说明前准备如下数据表:
#父表
CREATE TABLE IF NOT EXISTS class
(
classNo INT AUTO_INCREMENT,
classNature VARCHAR(100),
classHeadmaster VARCHAR(20)
);
#子表
create table blur_class_student
(
studentNO int primary key,
classNo INT,
studentName varchar(30),
foreign key(classNo) references class(classNo)
on update cascade
on delete cascade
);
#父表插入数据
INSERT INTO class VALUES(1,'scienceClass1','T_Ma');
INSERT INTO class VALUES(2,'literalClass1','T_Qin');
INSERT INTO class VALUES(3,'scienceClass2','T_Chou');
#子表插入数据
INSERT INTO blur_class_student VALUES (12,1,'zhao1');
INSERT INTO blur_class_student VALUES (23,2,'wang5');
INSERT INTO blur_class_student VALUES (25,2,'ma8');
INSERT INTO blur_class_student VALUES (33,3,'sun9');
INSERT INTO blur_class_student VALUES (55,5,'sdd');#此行为验证外键约束,执行将会报错
INSERT INTO blur_class_student VALUES (13,1,'qin4');
INSERT INTO blur_class_student VALUES (32,3,'li2');
1.基础查询语句
查询表的全部内容:
SELECT*FROM blur_class_student;
SELECT*FROM class;
按照字段查询:
SELECT studentName FROM blur_class_student;
奇怪的查询方式
SELECT 1,2*3;
查询结果如下
2. 带条件查询
查询前提前录入数据:
update blur_class_student set studentScore=99 where studentNo=12;
update blur_class_student set studentScore=60 where studentNo=13;
update blur_class_student set studentScore=98 where studentNo=23;
update blur_class_student set studentScore=88 where studentNo=25;
update blur_class_student set studentScore=86 where studentNo=32;
update blur_class_student set studentScore=80 where studentNo=33;
带条件查询格式
select* from table_name where column_condition;
column_condition可以的条件包括:
#大于等于、大于、小于、小于等于
#不等于,between……an……
#多个条件可以用 and or 连接
实例
select*from blur_class_student where studentScore>80;
SELECT*FROM blur_class_student WHERE studentScore>95 OR studentScore<61;
3. 模糊查询
关键字: like
说明:
- % 用来进行模糊多个关键字
- _ 表示只模糊一个关键字
- %condition%带有condition的进行查询
实例
#为完善功能新插入的数据
INSERT INTO blur_class_student VALUES(11,1,'li5',50);
INSERT INTO blur_class_student VALUES(14,1,'li8',77);
INSERT INTO blur_class_student VALUES(15,1,'sun4',87);
INSERT INTO blur_class_student VALUES(16,1,'li34',99);
INSERT INTO blur_class_student VALUES(17,1,'li386',60);
#模糊查询--%
SELECT*FROM blur_class_student WHERE studentName LIKE 'li%';
#模糊查询--_
SELECT*FROM blur_class_student WHERE studentName LIKE 'li_';
#模糊查询--%condition%
SELECT*FROM blur_class_student WHERE studentName LIKE '%s%';
4.结果排序
关键字: oder by asc(desc)
格式:
select* from table_name order by column ASC(DESC);//asc升序 desc降序
#为完成查询准备数据
ALTER TABLE blur_class_student ADD studentSex ENUM('men','women') DEFAULT 'men';
UPDATE blur_class_student SET studentSex='women' WHERE studentNo=12;
UPDATE blur_class_student SET studentSex='women' WHERE studentNo=16;
UPDATE blur_class_student SET studentSex='women' WHERE studentNo=17;
UPDATE blur_class_student SET studentSex='women' WHERE studentNo=25;
#用法
SELECT *FROM blur_class_student ORDER BY studentSex ASC,studentName DESC;
5.去重查询
**作用:**可以在学生表中查询存在的班级
关键字: distinct
格式:
select distinct column_name from table_name;
实例:
select distinct classNo from blur_class_student;
6.分页查询
(就是把某个范围内的值查询出来)
说明,MySQL的关键是是limit其他数据库为top
格式:
SELECT *FROM table_name LIMIT condition;
实例
#查询前三项
select *from blur_class_student limit 3;
#查询从第二项到第四项
select*from blur_class_student limit 2,4;
7.合并查询
关键字: union 或者 union all
格式-union:
说明:union默认操作符选取不同的值
select column_name from table_name1
union
select column_name from table_name2;
格式-union all
select column_name from table_name1
union all
select column_name from table_name2;
(简单的说就是union会把两个表中所有的内容不重复的显示出来,而不管该数据是不是在两个表中都存在,而union all 不仅可以显示相同内容,而重复内容也可以一起显示;与distinct的区别是distinct只能查询一个表)
实例:
#为实现应用,添加数据
insert into class values(4,'scienceClass2','T_Li');
insert into class values(5,'scienceClass3','T_Gao');
insert into class values(6,'scienceClass4','T_Wang');
#union
select classNo from class
union
select classNo from blur_class_student;
#union all
select classNo from class
union all
select classNo from blur_class_student;
查询结果
8.取别名
作用: 给表或者列取别名,可以达到易读的目的
关键字: as
格式:
#列的别名语法
select column_name as alias_name from table_name;
#表的别名语法
select alias_name.column_name1,alias_name.column_name2,…… from table_name as alias_name;
8.3 给列取别名
实例
SELECT studentName AS SN, studentScore AS SS FROM blur_class_student;
结果
请注意列名的变化
8.2 给表取一个别名
如果在一个语句中,表的名称过长,这样会降低可读性,及等等,因此如果能给表取别名可以简化操作
实例:
#原本学生表名字为 blur_class_student 现在更换为 b 则 b.classNo 的书写简单明了
SELECT b.`classNo`,b.`studentName` FROM blur_class_student AS b;
9.多表查询
格式:
select 字段1,字段2,…… from table1,table2…… where condition;
说明: 如果不加条件直接查询则结果为笛卡尔积
笛卡尔积实例
SELECT * FROM class,blur_class_student;
笛卡尔积结果
查询两个表中相互关联的实例
select* from class,blur_class_student where class.`classNo`=blur_class_student.`classNo`;
查询结果
如何理解两个表相互关联的查询
即查询两个表的共同内容,图片截取自图片来源
10. 多表连接查询
()
格式:
select [table_name1.column_name,table_name2.column_name]
from table1
inner|left|right join 表2 on 表1.字段=表2.字段;
10.1 内连接查询(只显示符合条件的数据)
实例
SELECT* FROM class INNER JOIN blur_class_student ON class.`classNo`=blur_class_student.`classNo`;
查询结果
查询说明
查询到的数据如下
10.2左外连接查询
实例
SELECT class.`classHeadmaster`,blur_class_student.`studentName`
FROM class LEFT JOIN blur_class_student ON class.`classNo`=blur_class_student.`classNo`;
结果
查询到的数据理解
10.3 右外连接查询
实例
SELECT class.`classHeadmaster`,blur_class_student.`studentName`
FROM class RIGHT JOIN blur_class_student ON class.`classNo`=blur_class_student.`classNo`;
查询结果
10.4 全连接查询
(说明:MySQL不支持全连接,但是可以用union实现full join 功能)