【MySQL】一文带你了解表的增删改查 CRUD

1. 增加(Create)

语法
insert [into] table_name [(column [, column] …)] values (value_list) [, (value_list)] …
可能有点复杂,其实很简单,下面有分部的简单的解释。

1.1 单行插入 + 全列插入

-- 插入两条记录,value_list 数量必须和定义表的列的数量及顺序一致
insert books values('道诡异仙','狐尾的笔','259');
insert into books values('夜的命名术','会说话的肘子','239');

增加

1.2 多行插入 + 指定列插入

-- 插入两条记录,value_list 数量必须和指定列数量及顺序一致
insert into books(name, author) values('诡秘之主','潜水的乌贼'),('牧神记','宅猪');

cr

2. 查询(Retrieve)

语法
select [distinct] {* | column [, column] …} [from table_name] [where …] [order by column [asc | desc], …] limit …

2.1 全列查询

-- 谨慎使用 * 进行全列查询哦~
-- 1. 查询的列越多,意味着需要传输的数据量越大;
-- 2. 可能会影响到索引的使用。
select * from books;

查询

2.2 指定列查询

-- 指定列的顺序不需要按定义表的顺序来
select name, author from books;

插入指定

2.3 查询字段为表达式

-- 可以进行四则表达式
-- 更改的知识自己的客服端,当再一次查询的时候,仍为未运算时;
select name, price + 10 from books;

运算

2.4 别名

语法
select column [as] alias_name […] from table_name;

-- 当运算后,表中名字会很怪,例如上图价格不在时price,而是price + 10
-- 这个时候我们就可以起个别名,让它变得简单
select name, price + 10 as up from books;

加价

2.5 去重

语法
select distinct column from table_name;

select distinct author from books;

quc

2.6 排序

语法
select … from table_name [where…] order by column [ASC|DESC], […];

-- 升序
select * from books order by price;
--降序
select * from books order by price desc;

排序

3. 条件查询(Where)

3.1比较运算符

运算符 说明
>, >=, <, <= 大于,大于等于,小于,小于等于
= 等于,null不安全,如:null = null 结果null
<=> 等于,null安全,如:null = null 结果true(1)
!=,<> 不等于
between a0 and a1 范围匹配,[a0, a1],如果 a0 <= value <= a1,返回 true(1)
in(option …) 如果是 option 中的任意一个,返回 true(1)
is null 是null
is not null 不是null
like 模糊匹配。% 表示任意多个(包括 0 个)任意字符;_ 表示任意一个字符

3.2 逻辑运算符

运算符 说明
and 多个条件必须都为 TRUE(1),结果才是 TRUE(1)
or 任意一个条件为 TRUE(1), 结果为 TRUE(1)
not 条件为 TRUE(1),结果为 FALSE(0)

3.3 举例

1. > <

-- 查询价格在300以上
select * from books where price > 300;
-- 查询价格在300以下
select * from books where price < 300;  

><

2. and or 和 is null is not null

相当于&& ||

--查询作者是null并且价格为null
select * from books where author is null and price is null;
--查询作者是null或者价格不是null
select * from books where author is null or price is not null;

筛选

3. between … and …

-- 查询价格在280-320
select * from books where price between 280 and 320; 

280

4. in

-- 查询价格是 299 或者 359 或者 398 或者 399 的书
select name, price from books where price in(299, 359, 398,399);

in

5. like

-- % 匹配任意多个(包括 0 个)字符
select name, price from books where price like '2%';
-- _ 匹配严格的一个任意字符
select name, price from books where price like '3_';

like

4. 分页查询(limit)

语法

-- 起始下标为 0
-- 从 0 开始,筛选 n 条结果
select ... from table_name [where ...] [order by ...] limit n;
-- 从 s 开始,筛选 n 条结果
select ... from table_name [where ...] [order by ...] limit s, n;
-- 从 s 开始,筛选 n 条结果,比第二种用法更明确,建议使用
select ... from table_name [where ...] [order by ...] limit n offset s;
-- 按价格分页,每页三条,分4页
-- 第一页
select * from books order by price limit 3 offset 3;
-- 第二页
select * from books order by price limit 3 offset 3;
-- 第三页
select * from books order by price limit 3 offset 6;
-- 第四页 不足三个无影响
select * from books order by price limit 3 offset 9;

分页

5. 修改(Update)

语法
update table_name set column = expr [, column = expr …] [where …] [order by …] [limit …]

-- 将斗罗大陆的作者改为唐家三少
update books set author = '唐家三少' where name = '斗罗大陆';
-- 将所有书降价10

修改
降价

6. 删除(Delete)

语法
delete from table_name [where …] [order by …] [limit …]

-- 删除斗破苍穹
delete from books where name = '斗破苍穹';
-- 删除整表数据
--谨慎操作
delete from books;
-- 但是表仍然存在
-- 删除整个表
drop table books;

shanchu

7. 总结

就是表的增删改查CRUD, 即增加(Create)、查询(Retrieve)、更新(Update)、删除(Delete)四个单词的首字母缩写。
这些属于基础的操作,下面还有更复杂的操作,我们下次见!

猜你喜欢

转载自blog.csdn.net/weixin_73392477/article/details/131028937