它是SQL中最核心的部分!可难可易!
SELECT [ALL|DISTINCT]
{* | 表名.* | 表名.字段名.... [AS 别名]}
FROM 要查询的表名 [AS 别名]
[LEFT | RIGHT | INNER JOIN 连接表名 [AS 别名]]
WHERE 查询条件;
简单理解就是你想查看哪些信息 例如:我想查看student表中的学生名、学生年龄、学生性别信息
1.查看学生表中的所有信息(不推荐写* 推荐写全字段名年列表) 效率低
SELECT * FROM student;
查看学生表中的学生姓名和性别
SELECT stuName,gender FROM student;
1.2 别名(比较常用) 注意: 此处使用中文只不过是方便你理解而已 后面不会采用写中文别名
AS关键字可以省略 ''也可以省略
SELECT stu.stuName AS '姓名',stu.gradeId + 1 年级 FROM student stu;
如果SQL过长可以进行"美化"
目前个人推荐写法
SELECT
stuName AS '姓名',
gradeId + 1 年级
FROM
student;
SELECT ALL stuName,gradeId FROM student;
DISTINCT 用来在指定的查询字段值范围内 去除重复数据
SELECT DISTINCT stuName,gradeId FROM student;
SELECT DISTINCT stuName,gender,gradeId FROM student;
在修改 和 删除时用过 目的是为了防止修改/删除全表。
用于检索数据表中符合条件的记录的
简单理解: 上方的操作是用来筛选列的 where是用来筛选行的
<关系/比较运算符和逻辑运算符>
SELECT * FROM student;
SELECT * FROM student WHERE gradeId > 1;
复杂条件的处理: 逻辑运算符 与and 或or 非not
SELECT * FROM student WHERE stuName = '张三' AND gender = '女';
SELECT * FROM student WHERE gender = '女' OR gradeId = 3;
三种方法
SELECT * FROM student WHERE NOT gender = '女';
SELECT * FROM student WHERE gender <> '女';
SELECT * FROM student WHERE gender != '女';
IS NULL IS NOT NULL
SELECT stuName FROM student WHERE address IS NULL;
SELECT stuName FROM student WHERE address IS NOT NULL;
SELECT stuName FROM student WHERE gradeId >= 2 AND gradeId <= 3;
更加简洁
SELECT stuName FROM student WHERE gradeId BETWEEN 2 AND 3;
IN查询 在…内/里面
SELECT * FROM student WHERE gradeId = 1 OR gradeId = 3;
更加简洁
SELECT * FROM student WHERE gradeId IN (1,3);
SELECT * FROM student WHERE gradeId NOT IN (1,3);
LIKE 像…一样 喜欢
SELECT * FROM student WHERE stuName LIKE '张%';
SELECT * FROM student WHERE stuName LIKE '张_';
SELECT * FROM student WHERE stuName LIKE '%三%';
SELECT * FROM student WHERE stuName LIKE '_三%';
连接查询: 内连接查询、外连接查询、[自连接查询]
内连接: 显式内连接、隐式内连接
SELECT stuName,gradeId FROM student;
SELECT
stu.stuName,g.gradeName
FROM
student stu
INNER JOIN grade g
ON stu.gradeId = g.gradeId; # 关联条件
SELECT
stu.stuName,g.gradeName
FROM
student stu,grade g
SELECT
stu.stuName,g.gradeName
FROM
student stu,grade g
WHERE
stu.gradeId = g.gradeId;
SELECT
stu.stuName,sub.subjectName,r.result
FROM
student stu,`subject` sub,result r
WHERE
stu.stuId = r.stuId
AND
sub.subjectId = r.subjectId;
外连接: 左外连接、右外连接
SELECT
stu.stuName,g.gradeName
FROM
student stu LEFT OUTER JOIN grade g
ON stu.gradeId = g.gradeId;
SELECT
stu.stuName,g.gradeName
FROM
student stu RIGHT JOIN grade g
ON stu.gradeId = g.gradeId;
查询游戏名称和所属分类
SELECT
c1.categoryName,c2.categoryName
FROM
category c1,category c2
WHERE
c1.pid = c2.id
编写DQL一定要严格按照此语法的顺序来实现!
SELECT [ALL | DISTINCT] ALL表示查询出所有的内容 DISTINCT 去重
{* | 表名.* | 表名.字段名[ AS 别名][,...]} 指定查询出的字段的
FROM
表名[AS 别名][,表1... AS 别名]
[INNER | [LEFT | RIGHT] [OUTER] JOIN 另一张表名 [AS 别名] ON 关联条件]
[WHERE 条件]
[GROUP BY 分组字段[,...]]
[HAVING 给分组后的数据进行条件筛选]
[ORDER BY 排序字段[,...]]
[LIMIT [startIndex,]pageSize]
排序: 成绩、游戏排行榜(金额、战力、等级…)、音乐、销量
需求: 查询出学生姓名、课程名称、成绩 并且按照成绩进行降序排序
SELECT
stu.stuName,sub.subjectName,r.result
FROM
result r,student stu,`subject` sub
WHERE
r.stuId = stu.stuId AND r.subjectId = sub.subjectId
ORDER BY r.result DESC;
需求: 在上方基础进行改动 要求按照成绩进行降序 且如果成绩相同按照时间降序
SELECT
stu.stuName,sub.subjectName,r.result,r.examDate
FROM
result r,student stu,`subject` sub
WHERE
r.stuId = stu.stuId AND r.subjectId = sub.subjectId
ORDER BY
r.result DESC , r.examDate DESC; # (X) ORDER BY r.result,r.examDate DESC;
LIMIT offset,row;
SELECT
*
FROM
student
LIMIT 0,2; -- 如果offset是从0开始 那么可以省略 LIMIT 2
分页: 因为数据量比较大的时候 如果把所有数据显示在一页上
需求: 现在学生信息要求每页显示2条 想查看第一页的数据
SELECT * FROM student LIMIT 0,2;
SELECT * FROM student LIMIT 2,2;
SELECT * FROM student LIMIT 4,2;
startIndex = (currentPage - 1) * pageSize;
子查询: 在一个查询中又嵌套了其他的查询,那么嵌套的查询就被称为子查询,而外层的查询被称为父查询。
子查询可以任意嵌套!可以出现在INSERT UPDATE DELETE WHERE等中…
需求: 查询在高等数学考试中,成绩比张三高的学生的姓名信息
如果不考虑连表查询
SELECT
stuId
FROM
result
WHERE
subjectId = 1
AND
result > 86;
SELECT
subjectId
FROM
`subject`
WHERE
subjectName = '高等数学';
整合
SELECT
stuId
FROM
result
WHERE
subjectId = (
SELECT
subjectId
FROM
`subject`
WHERE
subjectName = '高等数学'
)
AND
result > 86;
SELECT
result
FROM
result
WHERE
stuId = (SELECT stuId FROM student WHERE stuName = '张三')
AND subjectId = (SELECT subjectId FROM `subject` WHERE subjectName = '高等数学');
SELECT
r.stuId,stu.stuName
FROM
result r,student stu
WHERE
r.stuId = stu.stuId
AND
subjectId = (
SELECT subjectId FROM `subject` WHERE subjectName = '高等数学'
)
AND
result > (
SELECT result FROM result WHERE stuId = (
SELECT stuId FROM student WHERE stuName = '张三'
)
AND subjectId = (
SELECT subjectId FROM `subject` WHERE subjectName = '高等数学'
)
);
要求查询学生姓名、年级名称
原来
SELECT stu.stuName,g.gradeName FROM student stu,grade g WHERE stu.gradeId = g.gradeId;
SELECT
stu.stuName,
(SELECT g.gradeName FROM grade g WHERE g.gradeId = stu.gradeId) AS gradeName
FROM
student stu;
SELECT CONCAT('这是','MySQL','数据库');
SELECT REPLACE('这是MySQL数据库','MySQL','Oracle');
SELECT LTRIM(' Hello World ');
SELECT RTRIM(' Hello World ');
SELECT LENGTH('Hello');
SELECT SUBSTR('Hello World',5);
SELECT SUBSTR('Hello World',5,3);
SELECT RAND()
;
SELECT CEIL(20.4);
SELECT FLOOR(20.7);
SELECT ROUND(20.5);
SELECT ROUND(20.76,1)
SELECT NOW();
SELECT CURRENT_DATE();
SELECT CURRENT_TIME();
SELECT MONTH(NOW());
SELECT YEAR(NOW());
SELECT DAY(NOW());
SELECT HOUR(NOW());
SELECT MINUTE(NOW());
SELECT SECOND(NOW());
SELECT DATE_FORMAT(NOW(),'%Y-%m-%d');
SELECT DATEDIFF(NOW(),'2019-05-25');
SELECT MAX(gradeId) FROM grade;
SELECT MIN(gradeId) FROM grade;
SELECT AVG(result) FROM result WHERE subjectId = 1;
SELECT SUM(result) FROM result WHERE subjectId = 1;
SELECT COUNT(*) FROM student;
SELECT COUNT(stuId) FROM student;
SELECT COUNT(1) FROM student;
注意事项: 在分组查询的查询字段中 不要出现与分组查询中无关的字段值
需求: 计算各个年级的学生人数
SELECT gradeId,COUNT(1) FROM student GROUP BY gradeId;
SELECT gradeId,COUNT(1),GROUP_CONCAT(stuName) FROM student GROUP BY gradeId;
SELECT gradeId,gender,COUNT(1) FROM student GROUP BY gradeId,gender;
需求: 查询年级的学生人数大于1人的年级名称
SELECT
stu.gradeId,count(1),g.gradeName
FROM
student stu,grade g
WHERE
stu.gradeId = g.gradeId
GROUP BY
gradeId
HAVING
count(1) > 1;
相同点: 都可以用来进行条件判断 筛选行数
不同点:
位置不同: WHERE是在分组之前 HAVING是在分组之后
条件筛选不同: WHERE是给FROM查询到的数据进行条件筛选 而HAVING是对分组后的数据进行筛选