MySQL(查询数据)狂神笔记

4 DQL查询数据

4.1 DQL

image
image

4.2 指定查询

image

语法:select 字段...from 表
有的时候,列名不是那么的见名知意,我们起别名 as

distinct 去除查询结果中重复的数据,只显示一条
--查询哪些同学参加了 考试
select * from result --查询全部考试成绩
select `studentNO `from result --查询有哪些同学参加了考试
--去重复数据
select distinct  `studentNO `from result
数据库的列可以做什么

image

数据库中的表达式:文本值,列,null,函数,计算表达式,系统变量...
select 表达式 from 表

4.3 where条件子句

逻辑运算符

image
image

模糊查询

image
image

image

4.4 连表查询

image

inner join
/*思路:
1.分析需求,查询的字段来自哪些表(连接查询)
2.确定使用哪种连接查询  7种
确定交叉点(这两个表中哪个数据相同)
3.判断条件:表1 字段x=表2 字段y
*/
select s.studentNo,studentName,SubjectNo,StudentResult
from student as s
inner join result as r
on s.studentNo=r.studentNo
right join
select s.studentNo,studentName,SubjectNo,StudentResult
from student as s  --as可以省略
right join result as r
on s.studentNo=r.studentNo
left join
select s.studentNo,studentName,SubjectNo,StudentResult
from student as s  --as可以省略
left join result as r
on s.studentNo=r.studentNo
操作 描述
inner join 如果表中至少有一个匹配,返回就行
left join 会从左表中返回所有的值,即使右表中没有匹配
right join 会从右表中返回所有的值,即使左表中没有匹配

join(连接的表) on(判断条件) 是连接查询
where 等值查询

image

自连接

核心:一张表拆成两张一样的表

image

4.5 分页和排序

--排序:升序ASC,降序DESC
--order by 通过哪个字段排序,怎么排
select s.studentNo,studentName,SubjectNo,StudentResult
from student as s  --as可以省略
left join result as r
on s.studentNo=r.studentNo
order by studentResult ASC

--分页:limit 起始值,页面大小
select s.studentNo,studentName,SubjectNo,StudentResult
from student as s  --as可以省略
left join result as r
on s.studentNo=r.studentNo
order by studentResult ASC
limit 0,5

image

4.6 子查询

where(是计算出来的)
本质:嵌套查询
image

4.7 分组和过滤

image

5 常用函数

5.1 常用函数但不常用

MySQL文档

5.2 聚合函数

函数名称 描述
count() 计数
sum() 求和
avg() 平均值
max() 最大值
min() 最小值

image
image

5.3 数据库级别的md5加密(扩展)

image
image

posted @ 2023-07-27 09:15  huihui不会写代码  阅读(25)  评论(0)    收藏  举报