简书作者:seay
Learn [已经过测试校验]
一、简单查询
二、别名/更名
三、子查询(嵌套查询)
四、聚合函数查询
五、分组查询
六、模糊查询
准备工作
创建数据库和数据库表
【SQLServer中自增使用IDENTITY(1,1),MySQL中自增使用PRIMARY KEY】
--创建学生表
CREATE TABLEStudents(
Idint NOT NULL PRIMARY KEY,
Namevarchar(20) NOT NULL,
Classvarchar(15) NOT NULL,
Gendervarchar(10) NULL,
Ageint NULL,
Phonevarchar(15) NULL,
Addressvarchar(100) NULL);--创建课程表
CREATE TABLECourses(
Idint NOT NULL PRIMARY KEYAUTO_INCREMENT,
Namevarchar(50) NOT NULL);--创建成绩表
CREATE TABLEScores(
Idint PRIMARY KEYAUTO_INCREMENT,
SIdint NOT NULL,
CIdint NOT NULL,
Gradesdecimal(5,2) NOT NULL,
IsPassedbit NOT NULL);--插入学生表基础数据
INSERT INTO Students (Id,Name,Class,Gender,Age,Phone,Address) VALUES(2016001,'小明','一班','男',20,'18817716611','北京');INSERT INTO Students (Id,Name,Class,Gender,Age,Phone,Address) VALUES(2016002,'小龙','一班','男',19,'18817716622','天津');INSERT INTO Students (Id,Name,Class,Gender,Age,Phone,Address) VALUES(2016003,'小王','二班','男',20,'18817716633','北京');INSERT INTO Students (Id,Name,Class,Gender,Age,Phone,Address) VALUES(2016004,'婷婷','一班','女',17,'18817716644','济南');INSERT INTO Students (Id,Name,Class,Gender,Age,Phone,Address) VALUES(2016005,'张三','一班','男',19,'18817716655','北京');INSERT INTO Students (Id,Name,Class,Gender,Age,Phone,Address) VALUES(2016006,'小赵','一班','男',20,'18817716666','北京');INSERT INTO Students (Id,Name,Class,Gender,Age,Phone,Address) VALUES(2016007,'丽丽','二班','女',18,'18817716677','北京');INSERT INTO Students (Id,Name,Class,Gender,Age,Phone,Address) VALUES(2016008,'花花','一班','女',19,'18817716688','沈阳');INSERT INTO Students (Id,Name,Class,Gender,Age,Phone,Address) VALUES(2016009,'静静','二班','女',20,'18817716699','北京');--插入课程表基础数据
INSERT INTO Courses(Name) VALUES('语文');INSERT INTO Courses(Name) VALUES('数学');INSERT INTO Courses(Name) VALUES('英语');--插入成绩表基础数据
INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016001,1,120,1);INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016001,2,70,0);INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016001,3,89,1);INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016002,1,90,1);INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016002,2,88,1);INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016002,3,96,1);INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016003,1,112,1);INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016003,3,102,1);INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016004,1,80,1);INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016004,2,86,1);INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016004,3,47,0);INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016005,1,87,1);INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016005,2,96,1);INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016005,3,68,0);INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016006,2,95,1);INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016006,3,100,1);INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016007,1,87,1);INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016007,2,57,0);INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016007,3,130,1);INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016008,1,89,1);INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016008,3,66,0);INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016009,1,97,1);INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016009,2,104,1);INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016009,3,68,1);
View Code
一、简单查询
简单查询只需要SELECT、FROM、WHERE3个关键字即可实现。
SELECT * FROMStudents;SELECT * FROM Students WHERE Class='一班';SELECT * FROM Students WHERE Class='一班' AND Age = 20;
二、别名/更名
语法
SELECT 字段名1 [AS] 别名 [,字段名1 AS 别名]… FROM
AS可以省略
当别名中含有非字母和下划线时或者是关键字时,需要加上单/双引号
示例
SELECT Id AS '学号',Name '姓名',Class ClassName FROM Students;
三、子查询(嵌套查询)
子查询也称嵌套查询,是指一个SELECT查询语句可以嵌入另一个SELECT查询语句之中。SQL中允许多级嵌套,子查询在实际使用中非常多。
连接查询:涉及两个及以上的表查询为连接查询。
--查询二班学生成绩
SELECT * FROM Scores WHERE SId IN(SELECT Id FROM Students WHERE Class='二班')
四、聚合函数查询
聚合函数:是一个值的集合为输入,返回单个值的函数。
SQL预定义了5个聚集函数:AVG(平均值)、MIN(最小值)、MAX(最大值)、SUM(求和)、COUNT(计数)。
具体的数据库还会预定义一些其他常用的函数,比如字符串相聚合函数、时间聚合函数……。
SELECT AVG(Age),MAX(Age),MIN(Age),SUM(Age),COUNT(Id) FROM Students;
五、分组查询
使用GROUP BY子句可进行分组查询
注意:分组查询的时候要在GROUP BY子句后面跟上所有查询字段的列表
HAVING子句
如果需要在分组前的数据进行限制,可以使用HAVING子句
HAVING子句只能与GROUP BY搭配使用
--根据班级分组查询各班平均年龄并且班级人数大于3人
SELECT Class,AVG(Age) FROM Students GROUP BY Class HAVING COUNT(Id)>3;
HAVING子句和WHERE的区别
WHERE语句在GROUP BY语句之前,SQL会在分组之前计算WHERE语句;
HAVING语句在GROUP BY语句之后,SQL会在分组之后计算HAVING语句。
六、模糊查询
语法
SELECT 字段列表 FROM 表名 WHERE 字段 LIKE ''
模糊查询是通过关键字
LIKE和通配符实现的
_:任何单个字符(一个'_'只匹配一个字符,多个字符就使用多个_)
%:包含零个或更多字符的任意字符串(匹配任意内容)
[]:指定范围的字符(只匹配[]内的字符)
[ ^]:不在指定范围的字符(只匹配除[]内的字符)
SQL中通配符可以混合使用
(一)单个字符匹配
SELECT * FROM Students WHERE Name LIKE '小_';SELECT * FROM Students WHERE Phone LIKE '188177166__';
(二)范围内查询
--查询所有手机号码结尾22的信息
SELECT * FROM Students WHERE Phone LIKE '%22';
(三)不在范围内查询
--查询所有手机号码不以22结尾的信息
SELECT * FROM Students WHERE Phone NOT LIKE '%22';