** mysql -DQL
-- 查询全部的学生 SELECT * FROM `student` -- 查询指定字段 SELECT studentno, studentname FROM student -- 别名 给结果起一个名字 AS 可以给字段起别名 也可以给表起别名 SELECT studentno AS 学号, studentname AS 学生姓名 FROM student AS S -- 函数 Concat(a, b) SELECT CONCAT('姓名:', studentname) AS 新名字 FROM student
去重 distinct -- 去除SELECT查询出来的结果中重复的数据,重复的数据只显示一条
-- 查询那些同学参加考试 SELECT * FROM result -- 查询全部成绩 SELECT studentno FROM result -- 查询全部考试成绩 SELECT DISTINCT studentno FROM result -- 发现重复数据去重
数据库的表达式
SELECT VERSION() -- 查询版本系统 (函数) SELECT 100*2-38 AS 计算结果 -- 用来计算你(运算) SELECT @@auto_increment_increment -- 查询自增的步长 (变量) -- 学生成绩 + 1 查看 SELECT studentno, studentresult + 1 as 提分后 FROM result
WHERE 条件子句
- and &&
- or ||
- not !
-- 学生成绩 95 - 100之间 SELECT studentno, studentresult FROM result WHERE studentresult>= 95 AND studentresult<= 100 SELECT studentno, studentresult FROM result WHERE studentresult BETWEEN 95 AND 100 -- 除了学号等于1000的学生成绩 SELECT studentno, studentresult FROM result WHERE studentno != 1000 SELECT studentno, studentresult FROM result WHERE NOT studentno = 1000
WHERE 模糊查询
- IS NULL --> a is nul
- IS NOT NULL --> a is not null
- BETWEEN --> a between b and c
- Like --> a like b
- In --> a in (a1, a2, a3...)
-- 查询行赵的同学 -- ****like 结合 %(代表0到任意个字符) _( 一个字符)**** SELECT studentno, studentname FROM student SELECT studentno, studentname FROM student WHERE studentname LIKE '赵%' SELECT studentno, studentname FROM student WHERE studentname LIKE '赵_' -- 查询名字中带有‘嘉’的同学 SELECT studentno, studentname FROM student WHERE studentname LIKE '%嘉%' -- ******in***** -- 查询1001 1002 1003 号学员 SELECT studentno, studentname FROM student WHERE studentno IN(1000, 1002, 1003) -- null not null SELECT studentno, studentname FROM student WHERE address = '' || address IS NULL SELECT studentno, studentname FROM student WHERE address IS NOT NULL && address != ''
连表查询
- inner join --> 如果表中至少有一个匹配,就返回行
- left join --> 会从左表中返回所有的值,即使右表中没有匹配
- right join --> 会从右表中返回所有的值,即使左表没有匹配
解释: JOIN (连接的表) ON (条件) 连接查询
WHERE 等值查询
-- INNER JOIN SELECT s.studentno, s.studentname, r.subjectno, r.studentresult FROM student AS s INNER JOIN result AS r WHERE s.studentno = r.studentno -- RIGHT JOIN SELECT s.studentno, s.studentname, r.subjectno, r.studentresult FROM student s RIGHT JOIN result r ON s.studentno = r.studentno -- LEFT JOIN SELECT s.studentno, s.studentname, r.subjectno, r.studentresult FROM student s LEFT JOIN result r ON s.studentno = r.studentno
-- 查询所有缺考的同学 SELECT s.studentno, s.studentname, r.subjectno, r.studentresult FROM student s LEFT JOIN result r ON s.studentno = r.studentno WHERE r.studentresult IS NULL
-- 查询参加了考试的同学信息: 学号,学生姓名,科目名,分数 /* 思路 1. 分析需求 分析字段来自哪几张表 student result 2.缺定使用哪种查询 确定交叉点(两个表中那个数据是相同的) 判断的条件: 学生表 studentno = 成绩表 学生表中的studentno */ SELECT s.studentno, s.studentname, r.subjectno, r.studentresult, sj.subjectname FROM student s RIGHT JOIN result r ON s.studentno = r.studentno INNER JOIN `subject` sj ON r.subjectno = sj.subjectno
order by 排序
- desc 降序
- asc 升序
SELECT s.studentno, s.studentname, r.subjectno, r.studentresult, sj.subjectname FROM student s RIGHT JOIN result r ON s.studentno = r.studentno INNER JOIN `subject` sj ON r.subjectno = sj.subjectno ORDER BY r.studentresult DESC
LIMIT 分页
- LIMIT 起始值 页面的大小
- 【pageNum 当前页 piageSize页面大小】
- (pageNum - 1)* piageSize, piageSize
-- LIMIT 起始值 页面的大小 -- 【pageNum 当前页 piageSize页面大小】 -- (pageNum - 1)* piageSize, piageSize SELECT s.studentno, s.studentname, r.subjectno, r.studentresult, sj.subjectname FROM student s RIGHT JOIN result r ON s.studentno = r.studentno INNER JOIN `subject` sj ON r.subjectno = sj.subjectno ORDER BY r.studentresult DESC LIMIT 0,3
子查询
-- 子查询 -- where(值是计算出来的) -- 本质:在where语句中嵌套一个查询语句 -- 1.查询数据库结构-1 的所有考试成绩(学号 科目编号 成绩) 降序排列 SELECT studentno, sub.subjectno, studentresult FROM result r INNER JOIN `subject` sub ON r.subjectno = sub.subjectno WHERE sub.subjectname = '数据库结构-1' ORDER BY studentresult DESC -- 子查询方式 SELECT studentno, subjectno, studentresult FROM result WHERE subjectno = ( SELECT subjectno FROM `subject` WHERE subjectname = '数据库结构-1' ) ORDER BY studentresult DESC
分组及过滤
-- 查询不同课程的平均分,最高分,最低分 -- 核心 根据不同的课程分组 SELECT sub.subjectname, gradename, AVG(studentresult) AS 平均分, MAX(studentresult), MIN(studentresult) FROM result r INNER JOIN `subject` sub ON sub.subjectno = r.subjectno INNER JOIN grade g ON g.gradeid = sub.gradeid GROUP BY sub.subjectno HAVING 平均分 > 80