数据操作
插入数据
插入数据记录的基本语法是:
insert into <table_name> (column1, column2, ...) values(value1, value2, ...);
如果对所有列插入数据,则列可以省略:
insert into <table_name> values(value1, value2, ...);
插入数据时,也可批量操作:
insert into <table_name> (column1, column2, ...) values(value1, value2, ...),(value1, value2, ...),...;
not null字段值必须给出。- 列的顺序可以不与表中字段的顺序保持一致。
- 值的顺序必须与列字段的顺序保持一致。
删除数据
删除表中所有数据:
delete from <table_name>;
按条件删除:
delete from <table_name> where condition;
// delete from students where stu_age=18;
使用 delete 删除是硬删除,即彻底从表中抹除符合条件的记录。
数据对于企业或应用来说是非常重要的,实际开发中通常会使用一个字段标记成软删除,记录还存在,只是不针对用户显示。
修改数据
修改单个字段:
update <table_name> set column=value where condition;
修改多个字段:
update <table_name> set column1=value1, column1=value2 where condition;
修改时,建议加上条件,否则会修改表中对应列的所有的记录。
查询数据
查询所有
这种方式会查询包含所有字段的所有记录,通常不建议。
select * from <table_name>;
特定字段
查询包含特定字段的所有数据。
select column1, column2 from <table_name>;
条件查询
查询时,可以根据 where 指定查询条件。具体条件可包含:
=、!=等于及不等于。
select stu_name,stu_age from students where stu_age = 21;
select stu_name,stu_age from students where stu_age != 21;
>、<、>=、<=值范围设定。
select stu_name,stu_age from students where stu_age > 20;
select stu_name,stu_age from students where stu_age <= 20;
and并列成立。
select stu_name,stu_age from students where stu_age > 17 and stu_age < 20;
or单一成立。
select stu_name,stu_age from students where stu_age > 17 or stu_age < 20;
between and与and类似,但更具语义化。
select stu_name,stu_age from students where stu_age between 17 and 20;
not between and表示between或and以外的范围。
select stu_name,stu_age from students where stu_age not between 17 and 20;
模糊查询
SQL 通常使用 like 进行模糊查询:
- 查询的关键字大小写不敏感。
%表示任意多个字符。_表示一个字符。
select stu_name,stu_age from students where stu_name like '%m%';
select stu_name,stu_age from students where stu_name like '%m1';
select stu_name,stu_age from students where stu_name like '1m%';
select stu_name,stu_age from students where stu_name like '_m';
字段别名
可通过 as 关键字对查询结果展示进行别名展示:
select stu_name as 姓名,stu_age as 年龄 from students;
列计算
为了方便查询,可以在查询的同时进行列计算:
select stu_name as 姓名,2025 - stu_age as 年龄 from students;
清除重复
distinct 可以清除重复的记录:
select distinct stu_name from students;
排序
排序可使用 order by 关键字,若不指定则默认是升序。
select * from students order by stu_age asc; // 默认
select * from students order by stu_age desc;
除了指定字段名称排序以外,还可按照字段在表中的顺序进行排列:
// 对表中的第一个字段排序
select * from students order by 1 desc;
字符串使用 Unicode 码顺序进行排序。
排序也支持多字段排序,第一个字段值相同时,则排序第二个字段,以此类推:
select * from students order by stu_name,stu_age desc;
聚合函数
数量计算
通过 count(column) 函数可以计算满足条件的记录数量:
select count(stu_name) from students where stu_age > 20;
count() 函数必须跟列名,否则会报错。
最大值
通过 max(column) 函数可以计算满足条件的记录的最大值:
select max(stu_age) from students where stu_age > 20;
最小值
通过 min(column) 函数可以计算满足条件的记录的最小值:
select min(stu_age) from students where stu_age > 20;
求和
通过 sum(column) 函数可以计算满足条件的记录的和:
select sum(stu_age) from students;
平均值
通过 avg(column) 函数可以计算满足条件的记录的平均值:
select avg(stu_age) from students;
时间函数
时间字段可通过字符串或时间函数进行设置。
字符串
字符串需要满足标准的格式 YYYY-mm-dd HH:ii:ss:
insert into students values('name',23,'desc','2025-03-01 19:02:40');
内置函数
MySQL 中内置的日期时间函数有:
now()当前时间。syedate()系统时间。curtime()当前时间。curdate()当前日期。
insert into students values('name',23,'desc',now());
insert into students values('name',23,'desc',syedate());
insert into students values('name',23,'desc',curtime());
insert into students values('name',23,'desc',curdate());
字符串函数
拼接
concat 可用来拼接字符串:
select concat(stu_name, '\n------') from students;
大小写
upper 和 lower 可用来进行大小写转换:
select upper(stu_name) from students;
select lower(stu_name) from students;
截取
substring(str, start, count) 可用来对字符串进行截取:
select substring(stu_name, 2, 2) from students;
正则替换
regexp_replace(str, regex) 可进行正则替换:
select regexp_replace(stu_name, '\\d$', '****') from students;
分组查询
group by
group by 表示以什么字段进行分组:
select stu_age from students group by stu_age;
通常配合一些聚合函数进行数据统计:
select stu_age,count(stu_age) from students group by stu_age;
select stu_age,avg(stu_age) from students group by stu_age;
select stu_age,sum(stu_age) from students group by stu_age;
having
where是针对数据库中的数据进行条件筛选。group by是针对条件筛选后的结果进行分组。having是针对分组后的结果再次进行条件筛选。having必须结合group by使用。where>group by>having>order by。
select stu_age, count(stu_age) from students where stu_age > 20 group by stu_age having count(stu_age) > 1;
分页查询
根据不同的页码筛选出特定数量的数据。MySQL 中,limit 用来分页,语法为 limit initialIndex, totalQueryingCount:
initialIndex从 0 开始,表示第几条记录的索引。totalQueryingCount一共限制查询几条记录。- 可理解为从第几条开始查询多少条。
select * from students limit 0,3;
select * from students limit 1,3;