mysql
1.增
insert into 表名 (字段名1,字段名2...) value (值1,值2...);
编辑结束后点击运行。
【注意】
1)字段名对应值,字段名和值数量和顺序一一对应。
2)主键id不需要设置,会自动增加
2.删
delete from 表名 where 查询条件;
【注意】如果没有where条件,默认删除整张表记录,别干傻事!
3.改
update 表名 set 字段名1=新值1,字段2=新值2 where 条件;
4.查
select 字段1,字段2...from 表名;
查询整张表全部数据:select * from 表名;一般不这么做,几乎没有场景需要这么做,而且极耗性能。
5.高级查询
1)比较操作符
比较操作符用于对字段进行比较约束,比如:age<20,name="张三"等等
bettween--比较两个值之间(或者日期之间)
select 字段1,字段2...from 表名 birthday bettween "2020-08-02" and "2020-09-03";
//查询这个日期内生日的数据
操作符 |
描述 |
实例 |
= |
等于,比较两端值是否相等 |
name = '张三' |
!= |
不等于,比较两端是否不等于 |
name != '张三' |
> |
大于,比较查询内容大于条件字段 |
age > 20 |
< |
小于,比较查询内容小于条件字段 |
age < 50 |
>= |
大等于,比较查询内容大于等于条件字段 |
age >= 20 |
<= |
小等于,比较查询内容小于等于字段 |
age <= 20 |
bettween |
比较在两个值之间(或日期之间) |
birthday between '2020-05-22' and '2020-07-25' |
not between |
比较不在两个值之间 |
age not between 30 and 50 |
in |
在限制范围的组内 |
name in ( '张三', '李四', '王麻子' ) |
not in |
不在限制范围的组内 |
name not in ('张三','李四','王麻子') |
is null |
为空 |
birthday is null |
is not null |
不为空 |
birthday is not null |
// 查询所有年龄小于25的记录
select name,age from stus where age < 25;
// 修改所有名字是张三的记录
update stus set name='新张三', age=25, where name = '张三';
// 删除所有age小于10岁的记录
delete from stus where age < 10;
// 查询年龄在1980-01-01到2020-01-01之间的记录(切记日期格式一定要和记录中的格式一致!)
select name, age, birthday from stus where birthday between '1980-01-01' and '2010-01-01';
// 查询年龄不在30到50这个范围的记录
select name,age,birthday from stus where age not between 30 and 50;
// 查询名字在('张三','李四','王麻子')中的记录
select name,age,birthday from stus where name in ('张三','李四','王麻子');
// 查询年龄不在(20, 30, 40)中的记录
select name, age, birthday from stus where age not in (20, 30, 40);
2)逻辑操作符
逻辑操作符可以让我们对查询条件的范围和数量进行组织操作,让查询条件更加丰富。
操作符 |
描述 |
实例 |
NOT或! |
逻辑非 |
where not name = '李四' 或 where name != '李四' |
AND |
逻辑与 |
where name='张三' and not age=37 |
OR |
逻辑或 |
where name='张三' or age=20 |
// 查询名字不是'李四'的所有记录
select name,age,birthday from stus where not name = '李四';
// 查询姓名为'张三',并且年龄为20的记录
select name,age,birthday from stus where name='张三' and age=20;
// 查询姓名为'张三'或者年龄为30的记录
select name,age,birthday from stus where name='张三' or age=30
3)模糊查询
MYSQL中用LIKE子句来进行模糊查询,LIKE子句进行模糊查询的时候一般搭配%使用,若不用%,则LIKE相当于=操作符
- %源码——代表以【源码】结尾
- 源码%——代表以【源码】开头
- %源码%——代表包含【源码】两个字
- 源码——类似 =
// %四 代表以四结尾
select name, age, birthday from stus where name like '%四';
// 四% 代表以四开头
select name, age, birthday from stus where name like '%四';
// %四% 代表包含四
select name, age, birthday from stus where name like '%四';
// 不使用%的话,like的作用类似于 =
select name, age, birthday from stus where name like '李四';
4)查询结果排序
有时候我们需要对查询结果排序(按数字大小,日期等等),mysql提供了一个排序的子句,order by
order by子句有两个关键字:
- ASC 代表升序排列
- DESC 代表降序排列
默认情况下,order by排列顺序为DESC,即降序排列
// 查询所有学生,按年龄降序排列
select name,age,birthday from stus order by age desc;
5)分页查询
在网页里,我们常常看到表格有分页,这其实就是数据库分页返回的结果。MYSQL为我们提供了limit子句来实现分页。
- currentPage 是前端发送的【当前第几页】
- pageSize是前端发送的【每页多少条数据】
- (currentPage - 1)* pageSize的意思是,跳过前面多少页的条数,往后获取pageSize条
select 字段1,字段2... from 表名 limit (currentPage - 1) * pageSize, pageSize;
本文作者:丶乔
本文链接:https://www.cnblogs.com/sclweb/p/17637789.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步