DML:增删改表中的数据
- 添加数据
- 语法:
insert into 表名(列名1,列名2……列名n) value(值1,值2……值n) - 注意:
- 列名和值要一一对应
- 如果表名后,不定义列名,则默认给所有列添加值
insert into 表名 value(值1,值2……值n) - 除了数字类型,其他类型需要使用引号(单双都可以)
- 语法:
- 删除数据
- 语法:
delete from 表名[where 条件] - 注意:
- 如果不加条件,则删除表中所有的记录
- 如果要删除所有记录
- 语法:
1.delete from 表名; --不推荐使用,有多少条记录就会执行多少次删除操作
2.TRUNCATE TABLE 表名; --推荐使用,效率更高先删除表,然后再创建一张空表
- 修改数据
- 语法:
update 表名 set 列名1 = 值1,列名2 = 值1,……[where 条件] - 注意:如果不加任何条件,则会将表中所有记录全部修改
- 语法:
DQL:查询表中的数据select * from 表名
- 语法:
select
字段列表
from
表名列表
where
条件列表
group by
分组字段
having
分组之后的条件
order by
排序
limit
分页限定
- 基础查询
- 多个字段的查询
select * from 表名 - 去除重复
select distinct * from 表名 - 计算列
select math,english,math+english from 表名- 一般可以使用四则运算计算一些列的值。(一般只会进行数值型的计算)
- ifnull(表达式1,表达式2):null参与的运算,计算结果都为null;表达式1:那个字段需要判断是否为null;如果为null后替换值
select ifnull(name,'no name') from person;
- 起别名
select math,english,math+english as 综合 from 表名- 注意:as:可以省略
- 多个字段的查询
- 条件查询:
- where字句后跟条件
- 运算符
+ >, < , <= , >= , = ,<>
+ between……and
+ in(集合)
+ like:模糊查询(占位符:1._:单个任意字符。2.%:多个任意字符)
+ is null
+ and 或 &&
+ or 或 ||
+ not 或 !
-- 查询年龄大于20岁
select * from stu where age >= 20;
-- 查询年龄等于20岁
select * from stu where age = 20;
-- 查询年龄不等于20岁
select * from stu where age != 20;
select * from stu where age <> 20;
-- 查询年龄大于等于20 小于等于30
select * from stu where age >= 20 && age <= 30;
select * from stu where age >= 20 and age <=30;
select * from stu where age between 20 and 20;
-- 查询年龄22岁,18岁,25岁的信息
select * from stu where age = 22 or age = 18 or age = 25;
select * from stu where age = 22 || age = 18 || age = 25;
select * from stu where age in(22,18,25);
-- 查询英语成绩为null
select * from stu where english is null;
-- 查询英语成绩不为null
select * from stu where english is not null;
-- 查询姓马的有哪些? like
select * from stu where name like "马%";
-- 查询姓名第二个字是化的人
select * from stu where name like "_化%";
-- 查询姓名是3个字的人
select * from stu where name like "___";
-- 查询姓名中包含德的人
select * from stu where name like "%德%";
- 排序查询
- 语法:
order by 排序字段1 排序方式1 , 排序字段2 排序方式2... - 排序方式:
- ASC:升序,默认的
- DESC:降序
- 注意:如果有多个排序条件,则当前边的条件值一样时,才会判断第二条件
- 语法:
select * from student order by math ASC;
- 聚合函数:将一列数据作为一个整体,进行纵向的计算
- count:计算个数
- 一般选择非空的列:主键
- count(*)
- max:计算最大值
- min:计算最小值
- sum:计算和
- avg:计算平均值
- 注意:聚合函数的计算,排除null值。
- 解决方案:1.选择不包含非空的列进行计算。2.IFNULL函数
- count:计算个数
select count(IFNULL(math,0)) from student
- 分组查询
- 语法:
group by 分组字段 - 注意:
- 分组之后查询的字段:分组字段,聚合函数
- where和having的区别?
1.where 在分组之前进行限定,如果不满足条件,则不参与分组。having在分组之后进行限定,如果不满足结果,则不会被查询出来
2.where后不可以跟聚合函数,having可以进行聚合函数的判断 - 语法:
-- 按照性别分组。分别查询男、女同学的平均分
select sex,AVG(math) from student group by sex;
-- 按照性别分组。分别查询男、女同学的平均分,人数
select sex,AVG(math),COUNT(id) from student group by sex;
-- 按照性别分组。分别查询男、女同学的平均分,人数 要求:分数低于70分的人,不参与分组
select sex,AVG(math),COUNT(id) from student where math >= 70 group by sex;
-- 按照性别分组。分别查询男、女同学的平均分,人数 要求:分数低于70分的人,不参与分组,分组之后。人数要大于2个人
select sex,AVG(math),COUNT(id) from student math >= 70 group by sex having count(id) > 2;
- 分页查询
- 语法:limit开始的索引,每页查询的条数
- 公式:开始的索引 = (当前的页码 - 1)*每页显示的条数
-- 每页显示3条数据
select * from student limit 0,3; -- 第1页
select * from student limit 3,3; -- 第2页
SQL操作精要
本文深入讲解SQL语言的基础操作,包括数据的增删改查、条件筛选、排序、聚合计算及分组查询等核心功能,旨在帮助读者掌握高效的数据管理技巧。
994

被折叠的 条评论
为什么被折叠?



