文章目录
MySQL表的增删查改
CRUD:Create(创建),Retrieve(检索),Update(更新),Delete(删除)
注意:下面有些MySQL语句可能会带有有->
,使用时候需要删除!
1、Create(创建)
语法:
insert [into] table 表名[(字段名1,字段名2...)] values(对应的字段的值);
案例:先创建students表。
mysql> create table students( -> id int unsigned primary key auto_increment, -> sn int not null unique comment '学号', -> name varchar(20) not null, -> qq varchar(20) -> ); Query OK, 0 rows affected (0.28 sec) mysql>
1.1、单行数据全列插入
mysql> insert into students values(1,11,'xp','1234'); Query OK, 1 row affected (0.04 sec) mysql> insert students(sn,name,qq) values(22,'xxp','3456'); Query OK, 1 row affected (0.00 sec) mysql> select * from students; +----+----+------+------+ | id | sn | name | qq | +----+----+------+------+ | 1 | 11 | xp | 1234 | | 2 | 22 | xxp | 3456 | +----+----+------+------+ 2 rows in set (0.00 sec) mysql>
可以看到插入第二条数据的时候,并没有指定id,这里就用到了之前的auto_increment关键字,自增。
1.2、多行数据指定列插入
mysql> insert into students(sn,name) values -> (33,'xxxp'), -> (44,'xpp'); Query OK, 2 rows affected (0.00 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> select * from students; +----+----+------+------+ | id | sn | name | qq | +----+----+------+------+ | 1 | 11 | xp | 1234 | | 2 | 22 | xxp | 3456 | | 3 | 33 | xxxp | NULL | | 4 | 44 | xpp | NULL | +----+----+------+------+ 4 rows in set (0.00 sec) mysql>
需要注意的是,不管是全列插入还是指定列插入,values的所填值一定要和前面指定列的顺序一致!
1.3、插入更新(update)
我们在对主键或者唯一键插入重复值的时候会报错。
mysql> insert into students(sn,name) values (33,'zz'); ERROR 1062 (23000): Duplicate entry '33' for key 'sn' mysql> insert into students(id,sn,name) values (4,55,'zzz'); ERROR 1062 (23000): Duplicate entry '4' for key 'PRIMARY' mysql>
如果我们想让新插入的值覆盖之前的值,可以使用update关键字。
语法:
insert [into] table 表名[(字段名1,字段名2...)] values(对应的字段的值) on duplicate key update 字段名1=该字段值,字段名2=该字段值..;
注意,更新值以update后面的值为准。
mysql> select * from students; +----+----+------+------+ | id | sn | name | qq | +----+----+------+------+ | 1 | 11 | xp | 1234 | | 2 | 22 | xxp | 3456 | | 3 | 33 | xxxp | NULL | | 4 | 44 | xpp | NULL | +----+----+------+------+ 4 rows in set (0.00 sec) mysql> insert into students(id,sn,name) values (4,55,'zzz') on duplicate key update id=4,sn=55,name='zzz'; Query OK, 2 rows affected (0.01 sec) mysql> select * from students; +----+----+------+------+ | id | sn | name | qq | +----+----+------+------+ | 1 | 11 | xp | 1234 | | 2 | 22 | xxp | 3456 | | 3 | 33 | xxxp | NULL | | 4 | 55 | zzz | NULL | +----+----+------+------+ 4 rows in set (0.00 sec) mysql> insert into students(id,sn,name) values (5,22,'zz') on duplicate key update id=6,sn=66,name='zzzz'; Query OK, 2 rows affected (0.00 sec) mysql> select * from students; +----+----+------+------+ | id | sn | name | qq | +----+----+------+------+ | 1 | 11 | xp | 1234 | | 3 | 33 | xxxp | NULL | | 4 | 55 | zzz | NULL | | 6 | 66 | zzzz | 3456 | +----+----+------+------+ 4 rows in set (0.00 sec) mysql>
可以看到第一次update的时候,id重复了,那更新后对应的内容除了id都换成新的了。
第一次update的时候,sn重复了,那更新后对应的所有内容换成新的了(因为update后面的数据全换了)。
-- 0 row affected: 表中有冲突数据,但冲突数据的值和 update 的值相等 -- 1 row affected: 表中没有冲突数据,数据被插入 -- 2 row affected: 表中有冲突数据,并且数据已经被更新
使用
select row_count();
可以查看受影响的行数。select row_count(); +-------------+ | row_count() | +-------------+ | 2 | +-------------+ 1 row in set (0.00 sec) mysql> ;
1.4、替换(replace)
语法:
repalce [into] 表名[(字段名1,字段名2...)] values(对应的字段的值);
效果和update差不多,但是用法比update简单。
-- 1 row affected: 表中没有冲突数据,数据被插入 -- 2 row affected: 表中有冲突数据,则删除后再插入
mysql> replace students(id,sn,name,qq) values(6,22,'xx','6789'); Query OK, 2 rows affected (0.00 sec) mysql> select * from students; +----+----+------+------+ | id | sn | name | qq | +----+----+------+------+ | 1 | 11 | xp | 1234 | | 3 | 33 | xxxp | NULL | | 4 | 55 | zzz | NULL | | 6 | 22 | xx | 6789 | +----+----+------+------+ 4 rows in set (0.00 sec) mysql>
2、Retrieve(检索)
主要就是select语法。
语法:
select [distinct] {* 或者 字段名1,字段名2...} from 表名 [where ...] [order by 字段名 [asc 或者 desc]] [limit ...];
其中distinct关键字是用来去重的,*表示全列查询,字段名1,字段名2…表示指定列查询,where关键字是用来筛选的,order by 是用来排序的,默认是升序(asc),降序是desc,limit是显示内容条数。
案例:
mysql> create table exam_result( -> id int unsigned primary key auto_increment, -> name varchar(20) not null comment '姓名', -> chinese float default 0.0 comment '语文成绩', -> math float default 0.0 comment '数学成绩', -> english float default 0.0 comment "英语成绩" -> ); Query OK, 0 rows affected (0.04 sec) mysql> insert into exam_result(name,chinese,math,english) values -> ('唐三藏', 67, 98, 56), -> ('孙悟空', 87, 78, 77), -> ('猪悟能', 88, 98, 90), -> ('曹孟德', 82, 84, 67), -> ('刘玄德', 55, 85, 45), -> ('孙权', 70, 73, 78), -> ('宋公明', 75, 65, 30); Query OK, 7 rows affected (0.01 sec) Records: 7 Duplicates: 0 Warnings: 0 mysql>
2.1、select列
2.1.1、全列查询
mysql> select * from exam_result; +----+-----------+---------+------+---------+ | id | name | chinese | math | english | +----+-----------+---------+------+---------+ | 1 | 唐三藏 | 67 | 98 | 56 | | 2 | 孙悟空 | 87 | 78 | 77 | | 3 | 猪悟能 | 88 | 98 | 90 | | 4 | 曹孟德 | 82 | 84 | 67 | | 5 | 刘玄德 | 55 | 85 | 45 | | 6 | 孙权 | 70 | 73 | 78 | | 7 | 宋公明 | 75 | 65 | 30 | +----+-----------+---------+------+---------+ 7 rows in set (0.00 sec) mysql>
通常情况下不建议使用 * 进行全列查询!原因如下:
查询的列越多,意味着需要传输的数据量越大;
可能会影响到索引的使用。(索引待后面博客讲解)
2.1.2、指定列查询
mysql> select id,name from exam_result; +----+-----------+ | id | name | +----+-----------+ | 1 | 唐三藏 | | 2 | 孙悟空 | | 3 | 猪悟能 | | 4 | 曹孟德 | | 5 | 刘玄德 | | 6 | 孙权 | | 7 | 宋公明 | +----+-----------+ 7 rows in set (0.00 sec) mysql>
可以看到,只查询出来了id和name。
2.1.3、查询字段为表达式
mysql> select id,name,1+1 from exam_result; +----+-----------+-----+ | id | name | 1+1 | +----+-----------+-----+ | 1 | 唐三藏 | 2 | | 2 | 孙悟空 | 2 | | 3 | 猪悟能 | 2 | | 4 | 曹孟德 | 2 | | 5 | 刘玄德 | 2 | | 6 | 孙权 | 2 | | 7 | 宋公明 | 2 | +----+-----------+-----+ 7 rows in set (0.00 sec) mysql>
也就是说,select是可以进行计算的。
mysql> select 1*1999,1999/2; +--------+----------+ | 1*1999 | 1999/2 | +--------+----------+ | 1999 | 999.5000 | +--------+----------+ 1 row in set (0.00 sec) mysql>
因此我们可以对查询出的结果进行计算。
mysql> select id,name,english+10,math+10,english+math from exam_result; +----+-----------+------------+---------+--------------+ | id | name | english+10 | math+10 | english+math | +----+-----------+------------+---------+--------------+ | 1 | 唐三藏 | 66 | 108 | 154 | | 2 | 孙悟空 | 87 | 88 | 155 | | 3 | 猪悟能 | 100 | 108 | 188 | | 4 | 曹孟德 | 77 | 94 | 151 | | 5 | 刘玄德 | 55 | 95 | 130 | | 6 | 孙权 | 88 | 83 | 151 | | 7 | 宋公明 | 40 | 75 | 95 | +----+-----------+------------+---------+--------------+ 7 rows in set (0.00 sec) mysql>
2.1.4、给查询结果指定别名
语法:
select 字段名 [as] 别名 from 表名;
比如给name指定别名为名字,math指定别名为数学。
mysql> select id,name 名字,math as 数学,english from exam_result; +----+-----------+--------+---------+ | id | 名字 | 数学 | english | +----+-----------+--------+---------+ | 1 | 唐三藏 | 98 | 56 | | 2 | 孙悟空 | 78 | 77 | | 3 | 猪悟能 | 98 | 90 | | 4 | 曹孟德 | 84 | 67 | | 5 | 刘玄德 | 85 | 45 | | 6 | 孙权 | 73 | 78 | | 7 | 宋公明 | 65 | 30 | +----+-----------+--------+---------+ 7 rows in set (0.00 sec) mysql>
2.1.5、结果去重(distinct)
使用关键字distinct。
这里数学成绩有重复分数。
mysql> select math from exam_result; +------+ | math | +------+ | 98 | | 78 | | 98 | | 84 | | 85 | | 73 | | 65 | +------+ 7 rows in set (0.00 sec) mysql> select distinct math from exam_result; +------+ | math | +------+ | 98 | | 78 | | 84 | | 85 | | 73 | | 65 | +------+ 6 rows in set (0.00 sec) mysql>
2.2、where条件
where是用来筛选满足条件的数据。
语法:
select [distinct] {* 或者 字段名1,字段名2...} from 表名 where ... [order by 字段名 [asc 或者 desc]] [limit ...];
比较运算符:
运算符 说明 =
等于,NULL 不安全,例如 NULL = NULL 的结果是 NULL <>
不等于 >
大于 <
小于 >=
大于等于 <=
小于等于 BETWEEN ... AND ...
在某个范围内 LIKE
模糊匹配,用于字符串匹配,%表示任意多个字符,_只能是一个字符 IN
在指定的多个值中 IS NULL
值为NULL IS NOT NULL
值不为NULL <=>
等于,NULL 安全,例如 NULL <=> NULL 的结果是 TRUE(1) 逻辑运算符:
运算符 说明 AND
且,所有条件都为真时返回真 OR
或,任一条件为真时返回真 NOT
非,将条件结果取反
2.2.1、英语成绩<60 的同学及英语成绩
mysql> select name,english from exam_result where english<60; +-----------+---------+ | name | english | +-----------+---------+ | 唐三藏 | 56 | | 刘玄德 | 45 | | 宋公明 | 30 | +-----------+---------+ 3 rows in set (0.00 sec) mysql>
2.2.2、语文成绩在 80到90分 的同学及语文成绩
可以使用between and,也可以使用and。
mysql> select name,chinese from exam_result where chinese>=80 and chinese <=90; +-----------+---------+ | name | chinese | +-----------+---------+ | 孙悟空 | 87 | | 猪悟能 | 88 | | 曹孟德 | 82 | +-----------+---------+ 3 rows in set (0.00 sec) mysql> select name,chinese from exam_result where chinese between 80 and 90; +-----------+---------+ | name | chinese | +-----------+---------+ | 孙悟空 | 87 | | 猪悟能 | 88 | | 曹孟德 | 82 | +-----------+---------+ 3 rows in set (0.00 sec) mysql>
可以看到两种方式查询的结果是一样的。
2.2.3、数学成绩是 58 或者 59 或者 98 或者 99分 的同学及数学成绩
可以使用
or
,也可以使用in
。mysql> select name,math from exam_result where math=58 or math=59 or math=98 or math= 99; +-----------+------+ | name | math | +-----------+------+ | 唐三藏 | 98 | | 猪悟能 | 98 | +-----------+------+ 2 rows in set (0.00 sec) mysql> select name,math from exam_result where math in(58,59,98,99); +-----------+------+ | name | math | +-----------+------+ | 唐三藏 | 98 | | 猪悟能 | 98 | +-----------+------+ 2 rows in set (0.01 sec) mysql>
可以看到查出的结果是一样的,但是明显
in
更好用。
2.3.4、姓孙的同学及孙某同学
查询姓孙的同学,也就是可以是孙某,也可以是孙某某。
查询孙某同学,只能是两个字的。
mysql> select name from exam_result where name like '孙%'; +-----------+ | name | +-----------+ | 孙悟空 | | 孙权 | +-----------+ 2 rows in set (0.00 sec) mysql> select name from exam_result where name like '孙_'; +--------+ | name | +--------+ | 孙权 | +--------+ 1 row in set (0.00 sec) mysql>
可以看到
_
是严格匹配一个字符的。
2.3.5、语文成绩好于英语成绩 的同学及语文和英语成绩
mysql> select name,chinese,english from exam_result where chinese > english; +-----------+---------+---------+ | name | chinese | english | +-----------+---------+---------+ | 唐三藏 | 67 | 56 | | 孙悟空 | 87 | 77 | | 曹孟德 | 82 | 67 | | 刘玄德 | 55 | 45 | | 宋公明 | 75 | 30 | +-----------+---------+---------+ 5 rows in set (0.00 sec) mysql>
2.3.6、总成绩在200分以下 的同学及总成绩
mysql> select name,chinese+math+english 总成绩 from exam_result where chinese+math+english<200; +-----------+-----------+ | name | 总成绩 | +-----------+-----------+ | 刘玄德 | 185 | | 宋公明 | 170 | +-----------+-----------+ 2 rows in set (0.00 sec) mysql>
注意:
- where 条件中使用表达式
- 别名不能用在 where 条件中
原因:因为语句执行顺序是先where再select。也就是先挑出来符合要求的数据,再进行后续的处理(查询)。
2.3.7、语文成绩>80分 且 不姓孙的同学及语文成绩
mysql> select name,chinese from exam_result where chinese>80 and name not like ' 孙%'; +-----------+---------+ | name | chinese | +-----------+---------+ | 猪悟能 | 88 | | 曹孟德 | 82 | +-----------+---------+ 2 rows in set (0.00 sec) mysql>
2.3.8、要求是孙某同学 或者 要求总成绩>200 并且 语文成绩<数学成绩 并且 英语成绩>80
mysql> select *,chinese+math+english 总分 from exam_result where name like '孙_' or (chinese+math+english > 200 and chinese < math and english > 80); +----+-----------+---------+------+---------+--------+ | id | name | chinese | math | english | 总分 | +----+-----------+---------+------+---------+--------+ | 3 | 猪悟能 | 88 | 98 | 90 | 276 | | 6 | 孙权 | 70 | 73 | 78 | 221 | +----+-----------+---------+------+---------+--------+ 2 rows in set (0.00 sec) mysql>
2.3.9、null的查询
mysql> select * from students; +----+----+------+------+ | id | sn | name | qq | +----+----+------+------+ | 1 | 11 | xp | 1234 | | 3 | 33 | xxxp | NULL | | 4 | 55 | zzz | NULL | | 6 | 22 | xx | 6789 | +----+----+------+------+ 4 rows in set (0.00 sec) mysql> select name,qq from students where qq is not null; +------+------+ | name | qq | +------+------+ | xp | 1234 | | xx | 6789 | +------+------+ 2 rows in set (0.00 sec) mysql>
null
是不参与计算的。mysql> select null+1; +--------+ | null+1 | +--------+ | NULL | +--------+ 1 row in set (0.00 sec) mysql> select null+null; +-----------+ | null+null | +-----------+ | NULL | +-----------+ 1 row in set (0.00 sec) mysql>
null只参加使用
<=>
的比较,=
是查询不出结果的。mysql> select null <=> null; +---------------+ | null <=> null | +---------------+ | 1 | +---------------+ 1 row in set (0.00 sec) mysql> select null <=> 1; +------------+ | null <=> 1 | +------------+ | 0 | +------------+ 1 row in set (0.00 sec) mysql> select null = null; +-------------+ | null = null | +-------------+ | NULL | +-------------+ 1 row in set (0.00 sec) mysql>
2.3、结果排序
语法:
select [distinct] {* 或者 字段名1,字段名2...} from 表名 [where ...] order by 字段名 [asc 或者 desc] [limit ...]; -- asc表示升序,desc表示降序,不加这个关键字默认升序
注意:没有 ORDER BY 子句的查询,返回的顺序是未定义的,永远不要依赖这个顺序。
2.3.1、同学及语文成绩,按语文成绩升序
mysql> select name,chinese from exam_result order by chinese asc; +-----------+---------+ | name | chinese | +-----------+---------+ | 刘玄德 | 55 | | 唐三藏 | 67 | | 孙权 | 70 | | 宋公明 | 75 | | 曹孟德 | 82 | | 孙悟空 | 87 | | 猪悟能 | 88 | +-----------+---------+ 7 rows in set (0.02 sec) mysql> select name,chinese from exam_result order by chinese; +-----------+---------+ | name | chinese | +-----------+---------+ | 刘玄德 | 55 | | 唐三藏 | 67 | | 孙权 | 70 | | 宋公明 | 75 | | 曹孟德 | 82 | | 孙悟空 | 87 | | 猪悟能 | 88 | +-----------+---------+ 7 rows in set (0.00 sec) mysql>
可以看到,order by后默认是升序的。
2.3.2、同学及qq号,按qq号升序
mysql> select name,qq from students order by qq asc; +------+------+ | name | qq | +------+------+ | xxxp | NULL | | zzz | NULL | | xp | 1234 | | xx | 6789 | +------+------+ 4 rows in set (0.00 sec) mysql>
注意:NULL视为比任何值都小。
2.3.3、查询同学各门成绩,依次按数学降序,英语升序,语文升序
mysql> select * from exam_result order by math desc,english asc,chinese asc; +----+-----------+---------+------+---------+ | id | name | chinese | math | english | +----+-----------+---------+------+---------+ | 1 | 唐三藏 | 67 | 98 | 56 | | 3 | 猪悟能 | 88 | 98 | 90 | | 5 | 刘玄德 | 55 | 85 | 45 | | 4 | 曹孟德 | 82 | 84 | 67 | | 2 | 孙悟空 | 87 | 78 | 77 | | 6 | 孙权 | 70 | 73 | 78 | | 7 | 宋公明 | 75 | 65 | 30 | +----+-----------+---------+------+---------+ 7 rows in set (0.00 sec) mysql>
2.3.4、查询同学及总分由高到低
mysql> select name,chinese+math+english 总分 from exam_result order by chinese+math+english desc; +-----------+--------+ | name | 总分 | +-----------+--------+ | 猪悟能 | 276 | | 孙悟空 | 242 | | 曹孟德 | 233 | | 唐三藏 | 221 | | 孙权 | 221 | | 刘玄德 | 185 | | 宋公明 | 170 | +-----------+--------+ 7 rows in set (0.00 sec) mysql>
使用别名排序:
mysql> select name,chinese+math+english 总分 from exam_result order by 总分 desc; +-----------+--------+ | name | 总分 | +-----------+--------+ | 猪悟能 | 276 | | 孙悟空 | 242 | | 曹孟德 | 233 | | 唐三藏 | 221 | | 孙权 | 221 | | 刘玄德 | 185 | | 宋公明 | 170 | +-----------+--------+ 7 rows in set (0.00 sec) mysql>
是不是有疑问为什么这里order by 可以用别名排序?
因为order by优先级低于select。也就是说先选出所有人再排序!
2.3.5、查询姓孙的同学或者姓曹的同学数学成绩,结果按数学成绩由高到低显示
mysql> select name ,math from exam_result where name like '孙%' or name like '曹%' order by math desc; +-----------+------+ | name | math | +-----------+------+ | 曹孟德 | 84 | | 孙悟空 | 78 | | 孙权 | 73 | +-----------+------+ 3 rows in set (0.00 sec) mysql>
可以看到,这里结合了where条件。
2.4、筛选分页结果
语法:
-- 起始下标为 0 -- 从 0 开始,筛选 n 条结果 select [distinct] {* 或者 字段名1,字段名2...} from 表名 [where ...] [order by 字段名 [asc 或者 desc]] limit n; -- 从 s 开始,筛选 n 条结果 select [distinct] {* 或者 字段名1,字段名2...} from 表名 [where ...] [order by 字段名 [asc 或者 desc]] limit s, n; -- 从 s 开始,筛选 n 条结果,比第二种用法更明确,建议使用 select [distinct] {* 或者 字段名1,字段名2...} from 表名 [where ...] [order by 字段名 [asc 或者 desc]] limit n offset s;
建议:对未知表进行查询时,最好加一条 limit 1,避免因为表中数据过大,查询全表数据导致数据库卡死!
案例:按id升序进行分页,每页3条记录
mysql> select * from exam_result order by id limit 3 offset 0; +----+-----------+---------+------+---------+ | id | name | chinese | math | english | +----+-----------+---------+------+---------+ | 1 | 唐三藏 | 67 | 98 | 56 | | 2 | 孙悟空 | 87 | 78 | 77 | | 3 | 猪悟能 | 88 | 98 | 90 | +----+-----------+---------+------+---------+ 3 rows in set (0.00 sec) mysql> select * from exam_result order by id limit 3 offset 3; +----+-----------+---------+------+---------+ | id | name | chinese | math | english | +----+-----------+---------+------+---------+ | 4 | 曹孟德 | 82 | 84 | 67 | | 5 | 刘玄德 | 55 | 85 | 45 | | 6 | 孙权 | 70 | 73 | 78 | +----+-----------+---------+------+---------+ 3 rows in set (0.00 sec) mysql> select * from exam_result order by id limit 3 offset 6; +----+-----------+---------+------+---------+ | id | name | chinese | math | english | +----+-----------+---------+------+---------+ | 7 | 宋公明 | 75 | 65 | 30 | +----+-----------+---------+------+---------+ 1 row in set (0.00 sec) mysql>
可以看到,这里分了三页显示。
3、Update(更新)
语法:
update 表名 set 字段名1=更新值,... [where ...] [order by 字段名 [asc 或者 desc]] [limit ...];
update对查询到的结果进行列(字段)值更新。
3.1、将孙悟空同学的数学成绩变更为 80 分
mysql> select name, math from exam_result where name='孙悟空'; +-----------+------+ | name | math | +-----------+------+ | 孙悟空 | 78 | +-----------+------+ 1 row in set (0.00 sec) mysql> update exam_result set math=80 where name='孙悟空'; Query OK, 1 row affected (0.02 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select name, math from exam_result where name='孙悟空'; +-----------+------+ | name | math | +-----------+------+ | 孙悟空 | 80 | +-----------+------+ 1 row in set (0.00 sec) mysql>
3.2、将曹孟德同学的数学成绩变更为 60 分,语文成绩变更为 70 分
mysql> select name, math, chinese from exam_result where name='曹孟德'; +-----------+------+---------+ | name | math | chinese | +-----------+------+---------+ | 曹孟德 | 84 | 82 | +-----------+------+---------+ 1 row in set (0.00 sec) mysql> update exam_result set math=60,chinese=70 where name='曹孟德'; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select name, math, chinese from exam_result where name='曹孟德'; +-----------+------+---------+ | name | math | chinese | +-----------+------+---------+ | 曹孟德 | 60 | 70 | +-----------+------+---------+ 1 row in set (0.00 sec) mysql>
3.3、将总成绩倒数前三的 3 位同学的数学成绩加上 30 分
注意:数据更新,不支持 math += 30 这种语法。
mysql> select name,math,chinese+math+english 总分 from exam_result order by 总分; +-----------+------+--------+ | name | math | 总分 | +-----------+------+--------+ | 宋公明 | 65 | 170 | | 刘玄德 | 85 | 185 | | 曹孟德 | 60 | 197 | | 唐三藏 | 98 | 221 | | 孙权 | 73 | 221 | | 孙悟空 | 80 | 244 | | 猪悟能 | 98 | 276 | +-----------+------+--------+ 7 rows in set (0.00 sec) mysql> update exam_result set math=math+30 order by chinese+math+english limit 3; Query OK, 3 rows affected (0.01 sec) Rows matched: 3 Changed: 3 Warnings: 0 mysql> select name,math,chinese+math+english 总分 from exam_result order by 总分; +-----------+------+--------+ | name | math | 总分 | +-----------+------+--------+ | 宋公明 | 95 | 200 | | 刘玄德 | 115 | 215 | | 唐三藏 | 98 | 221 | | 孙权 | 73 | 221 | | 曹孟德 | 90 | 227 | | 孙悟空 | 80 | 244 | | 猪悟能 | 98 | 276 | +-----------+------+--------+ 7 rows in set (0.00 sec) mysql>
3.4、将所有同学的语文成绩更新为原来的 2 倍
注意:更新全表的语句一定要慎用!
mysql> select name,chinese from exam_result; +-----------+---------+ | name | chinese | +-----------+---------+ | 唐三藏 | 67 | | 孙悟空 | 87 | | 猪悟能 | 88 | | 曹孟德 | 70 | | 刘玄德 | 55 | | 孙权 | 70 | | 宋公明 | 75 | +-----------+---------+ 7 rows in set (0.00 sec) mysql> update exam_result set chinese=chinese*2; Query OK, 7 rows affected (0.00 sec) Rows matched: 7 Changed: 7 Warnings: 0 mysql> select name,chinese from exam_result; +-----------+---------+ | name | chinese | +-----------+---------+ | 唐三藏 | 134 | | 孙悟空 | 174 | | 猪悟能 | 176 | | 曹孟德 | 140 | | 刘玄德 | 110 | | 孙权 | 140 | | 宋公明 | 150 | +-----------+---------+ 7 rows in set (0.00 sec) mysql>
4、Delete(删除)
4.1、删除数据
语法:
delete from 表名 [where ...] [order by 字段名 [asc 或者 desc]] [limit ...];
4.1.1、 删除孙悟空同学的考试成绩
mysql> select * from exam_result where name='孙悟空'; +----+-----------+---------+------+---------+ | id | name | chinese | math | english | +----+-----------+---------+------+---------+ | 2 | 孙悟空 | 174 | 80 | 77 | +----+-----------+---------+------+---------+ 1 row in set (0.00 sec) mysql> delete from exam_result where name='孙悟空'; Query OK, 1 row affected (0.00 sec) mysql> select * from exam_result where name='孙悟空'; Empty set (0.00 sec) mysql>
4.1.2、删除整张表数据
注意:删除整表操作要慎用!
-- 准备测试表 CREATE TABLE for_delete ( id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(20) ); Query OK, 0 rows affected (0.16 sec) -- 插入测试数据 INSERT INTO for_delete (name) VALUES ('A'), ('B'), ('C'); Query OK, 3 rows affected (1.05 sec) Records: 3 Duplicates: 0 Warnings: 0 -- 查看测试数据 SELECT * FROM for_delete; +----+------+ | id | name | +----+------+ | 1 | A | | 2 | B | | 3 | C | +----+------+ 3 rows in set (0.00 sec)
mysql> INSERT INTO for_delete (name) VALUES ('D'); Query OK, 1 row affected (0.01 sec) mysql> SELECT * FROM for_delete; +----+------+ | id | name | +----+------+ | 4 | D | +----+------+ 1 row in set (0.00 sec) mysql> show create table for_delete; +------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | for_delete | CREATE TABLE `for_delete` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(20) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 | +------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) mysql>
我们需要注意的是,删除整张表只是删除数据,表结构并不删除,而且表的自增长还继承下来了!
4.2、截断表
语法:
truncate [table] 表名;
注意:这个操作慎用。
只能对整表操作,不能像 delete 一样针对部分数据操作
实际上 MySQL 不对数据操作,所以比 delete 更快,但是truncate在删除数据的时候,并不经过真正的事务(后面博客会讲),所以无法回滚
会重置 auto_increment 项
案例:
-- 准备测试表 CREATE TABLE for_truncate ( id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(20) ); Query OK, 0 rows affected (0.16 sec) -- 插入测试数据 INSERT INTO for_truncate (name) VALUES ('A'), ('B'), ('C'); Query OK, 3 rows affected (1.05 sec) Records: 3 Duplicates: 0 Warnings: 0 -- 查看测试数据 SELECT * FROM for_truncate; +----+------+ | id | name | +----+------+ | 1 | A | | 2 | B | | 3 | C | +----+------+ 3 rows in set (0.00 sec)
mysql> truncate table for_truncate; Query OK, 0 rows affected (0.02 sec) mysql>
可以看到这里truncate表后,影响行数是0,所以实际上没有对数据真正操作。
mysql> SELECT * FROM for_truncate; Empty set (0.00 sec) mysql> show create table for_truncate; +--------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +--------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | for_truncate | CREATE TABLE `for_truncate` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(20) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 | +--------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) mysql>
可以看到这里 auto_increment 项已经重置,插入会重新开始。
mysql> INSERT INTO for_truncate (name) VALUES('D'); Query OK, 1 row affected (0.01 sec) mysql> SELECT * FROM for_truncate; +----+------+ | id | name | +----+------+ | 1 | D | +----+------+ 1 row in set (0.00 sec) mysql> show create table for_truncate; +--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | for_truncate | CREATE TABLE `for_truncate` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(20) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 | +--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) mysql>
可以看到插入一条数据后,auto_increment自增长为2。
5、插入查询结果
语法:
insert into 表名 [字段名1,字段名2...] select ... from ...;
当我们需要对当前表的数据去重,直接使用distinct关键字只能用来筛选结果,并不改变原表数据。
这时候我们可以先对需要去重的表进行去重查询后插入到新表,再把新表命名为需要去重的表名!
案例:
-- 建表 mysql> CREATE TABLE duplicate_table (id int, name varchar(20)); Query OK, 0 rows affected (0.05 sec) -- 插入重复数据 mysql> INSERT INTO duplicate_table VALUES -> (100, 'aaa'), -> (100, 'aaa'), -> (200, 'bbb'), -> (200, 'bbb'), -> (200, 'bbb'), -> (300, 'ccc'); Query OK, 6 rows affected (0.01 sec) Records: 6 Duplicates: 0 Warnings: 0 -- 创建一个结构和duplicate_table的表 mysql> create table no_duplicate_table like duplicate_table; Query OK, 0 rows affected (0.03 sec) mysql> select * from no_duplicate_table; Empty set (0.00 sec) mysql> select * from duplicate_table; +------+------+ | id | name | +------+------+ | 100 | aaa | | 100 | aaa | | 200 | bbb | | 200 | bbb | | 200 | bbb | | 300 | ccc | +------+------+ 6 rows in set (0.00 sec) mysql> insert into no_duplicate_table select distinct * from duplicate; -- duplicate_table去重后的数据插入到no_duplicate_table mysql> select * from no_duplicate_table; -- 去重后的数据 +------+------+ | id | name | +------+------+ | 100 | aaa | | 200 | bbb | | 300 | ccc | +------+------+ 3 rows in set (0.00 sec) mysql> rename table duplicate_table to old_duplicate_table,no_duplicate_table to duplicate_table; -- 改名 Query OK, 0 rows affected (0.03 sec) mysql> select * from duplicate_table; +------+------+ | id | name | +------+------+ | 100 | aaa | | 200 | bbb | | 300 | ccc | +------+------+ 3 rows in set (0.00 sec) mysql>
6、聚合函数
聚合函数 说明 COUNT([DISTINCT] column)
返回满足条件的行数 SUM([DISTINCT] column)
返回指定列值的总和 AVG([DISTINCT] column)
返回指定列值的平均值 MAX(column)
返回指定列中的最大值 MIN(column)
返回指定列中的最小值 GROUP_CONCAT([DISTINCT] column)
将组中的值连接成一个字符串 VARIANCE([DISTINCT] column)
返回样本方差 STDDEV([DISTINCT] column)
返回样本标准偏差 BIT_AND(column)
按位与,并返回所有非空位的结果 BIT_OR(column)
按位或,并返回所有非空位的结果 BIT_XOR(column)
按位异或,并返回所有非空位的结果 JSON_ARRAYAGG(column)
将组中的值作为JSON数组返回 JSON_OBJECTAGG(key_column, value_column)
将组中的键值对作为JSON对象返回
6.1、统计班级共有多少同学
mysql> select * from students; +----+----+------+------+ | id | sn | name | qq | +----+----+------+------+ | 1 | 11 | xp | 1234 | | 3 | 33 | xxxp | NULL | | 4 | 55 | zzz | NULL | | 6 | 22 | xx | 6789 | +----+----+------+------+ 4 rows in set (0.00 sec) mysql> select count(*) from students; +----------+ | count(*) | +----------+ | 4 | +----------+ 1 row in set (0.02 sec) -- 用表达式做统计 mysql> select count(2+1) from students; +------------+ | count(2+1) | +------------+ | 4 | +------------+ 1 row in set (0.00 sec) mysql>
6.2、统计班级收集的 qq 号有多少
mysql> select count(qq) from students; +-----------+ | count(qq) | +-----------+ | 2 | +-----------+ 1 row in set (0.00 sec) mysql>
注意:NULL不参与统计
6.3、统计本次考试的数学成绩分数个数(不统计重复)
mysql> select * from exam_result; +----+-----------+---------+------+---------+ | id | name | chinese | math | english | +----+-----------+---------+------+---------+ | 1 | 唐三藏 | 134 | 98 | 56 | | 3 | 猪悟能 | 176 | 98 | 90 | | 4 | 曹孟德 | 140 | 90 | 67 | | 5 | 刘玄德 | 110 | 115 | 45 | | 6 | 孙权 | 140 | 73 | 78 | | 7 | 宋公明 | 150 | 95 | 30 | +----+-----------+---------+------+---------+ 6 rows in set (0.00 sec) mysql> select count(distinct math) from exam_result; +----------------------+ | count(distinct math) | +----------------------+ | 5 | +----------------------+ 1 row in set (0.00 sec) mysql>
6.4、统计数学成绩总分以及不及格的数学成绩总分
mysql> select sum(math) from exam_result; +-----------+ | sum(math) | +-----------+ | 569 | +-----------+ 1 row in set (0.00 sec) mysql> select sum(math) from exam_result where math < 60; +-----------+ | sum(math) | +-----------+ | NULL | +-----------+ 1 row in set (0.00 sec) mysql>
6.5、统计平均总分
mysql> select avg(math+chinese+english) from exam_result; +---------------------------+ | avg(math+chinese+english) | +---------------------------+ | 297.5 | +---------------------------+ 1 row in set (0.00 sec) mysql>
6.6、返回英语最高分
mysql> select max(english) from exam_result; +--------------+ | max(english) | +--------------+ | 90 | +--------------+ 1 row in set (0.00 sec) mysql>
6.7、返回 > 70 分以上的数学最低分
mysql> select min(math) from exam_result where math > 70; +-----------+ | min(math) | +-----------+ | 73 | +-----------+ 1 row in set (0.00 sec) mysql>
7、group by子句
语法:
select 字段1, 列2, .. from 表名 group by 字段;
案例:
以下是一个经典的雇员信息表(EMP表),这是Oracle 9i数据库中常用的测试表。我们将以MySQL语法来创建该表,并插入一些示例数据。
创建和插入语句的完整SQL:
CREATE TABLE EMP ( EMPNO INT NOT NULL, ENAME VARCHAR(10), JOB VARCHAR(9), MGR INT, HIREDATE DATE, SAL DECIMAL(7, 2), COMM DECIMAL(7, 2), DEPTNO INT, PRIMARY KEY (EMPNO) ); INSERT INTO EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO) VALUES (7369, 'SMITH', 'CLERK', 7902, '1980-12-17', 800.00, NULL, 20), (7499, 'ALLEN', 'SALESMAN', 7698, '1981-02-20', 1600.00, 300.00, 30), (7521, 'WARD', 'SALESMAN', 7698, '1981-02-22', 1250.00, 500.00, 30), (7566, 'JONES', 'MANAGER', 7839, '1981-04-02', 2975.00, NULL, 20), (7698, 'BLAKE', 'MANAGER', 7839, '1981-05-01', 2850.00, NULL, 30), (7782, 'CLARK', 'MANAGER', 7839, '1981-06-09', 2450.00, NULL, 10), (7788, 'SCOTT', 'ANALYST', 7566, '1982-12-09', 3000.00, NULL, 20), (7839, 'KING', 'PRESIDENT', NULL, '1981-11-17', 5000.00, NULL, 10), (7844, 'TURNER', 'SALESMAN', 7698, '1981-09-08', 1500.00, 0.00, 30), (7876, 'ADAMS', 'CLERK', 7788, '1983-01-12', 1100.00, NULL, 20), (7900, 'JAMES', 'CLERK', 7698, '1981-12-03', 950.00, NULL, 30), (7902, 'FORD', 'ANALYST', 7566, '1981-12-03', 3000.00, NULL, 20), (7934, 'MILLER', 'CLERK', 7782, '1982-01-23', 1300.00, NULL, 10);
插入后的数据:
mysql> select * from EMP; +-------+--------+-----------+------+------------+---------+--------+--------+ | EMPNO | ENAME | JOB | MGR | HIREDATE | SAL | COMM | DEPTNO | +-------+--------+-----------+------+------------+---------+--------+--------+ | 7369 | SMITH | CLERK | 7902 | 1980-12-17 | 800.00 | NULL | 20 | | 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600.00 | 300.00 | 30 | | 7521 | WARD | SALESMAN | 7698 | 1981-02-22 | 1250.00 | 500.00 | 30 | | 7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975.00 | NULL | 20 | | 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | NULL | 30 | | 7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450.00 | NULL | 10 | | 7788 | SCOTT | ANALYST | 7566 | 1982-12-09 | 3000.00 | NULL | 20 | | 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 5000.00 | NULL | 10 | | 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500.00 | 0.00 | 30 | | 7876 | ADAMS | CLERK | 7788 | 1983-01-12 | 1100.00 | NULL | 20 | | 7900 | JAMES | CLERK | 7698 | 1981-12-03 | 950.00 | NULL | 30 | | 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000.00 | NULL | 20 | | 7934 | MILLER | CLERK | 7782 | 1982-01-23 | 1300.00 | NULL | 10 | +-------+--------+-----------+------+------------+---------+--------+--------+ 13 rows in set (0.00 sec)
以上语句将在MySQL中创建一个雇员信息表,并插入一些示例数据。你可以直接将这些SQL语句复制到你的MySQL环境中运行。
- 显示每个部分的平均工资、最高工资和最低工资
mysql> select deptno,avg(sal),max(sal),min(sal) from EMP group by deptno; +--------+-------------+----------+----------+ | deptno | avg(sal) | max(sal) | min(sal) | +--------+-------------+----------+----------+ | 10 | 2916.666667 | 5000.00 | 1300.00 | | 20 | 2175.000000 | 3000.00 | 800.00 | | 30 | 1630.000000 | 2850.00 | 950.00 | +--------+-------------+----------+----------+ 3 rows in set (0.00 sec) mysql>
- 显示每个部门的每种岗位的平均工资和最低工资
mysql> select deptno,job,avg(sal),min(sal) from EMP group by deptno,job; +--------+-----------+-------------+----------+ | deptno | job | avg(sal) | min(sal) | +--------+-----------+-------------+----------+ | 10 | CLERK | 1300.000000 | 1300.00 | | 10 | MANAGER | 2450.000000 | 2450.00 | | 10 | PRESIDENT | 5000.000000 | 5000.00 | | 20 | ANALYST | 3000.000000 | 3000.00 | | 20 | CLERK | 950.000000 | 800.00 | | 20 | MANAGER | 2975.000000 | 2975.00 | | 30 | CLERK | 950.000000 | 950.00 | | 30 | MANAGER | 2850.000000 | 2850.00 | | 30 | SALESMAN | 1450.000000 | 1250.00 | +--------+-----------+-------------+----------+ 9 rows in set (0.00 sec) mysql>
显示平均工资低于2000的部门和它的平均工资
统计各部门的平均工资
mysql> select deptno,avg(sal) from EMP group by deptno; +--------+-------------+ | deptno | avg(sal) | +--------+-------------+ | 10 | 2916.666667 | | 20 | 2175.000000 | | 30 | 1630.000000 | +--------+-------------+ 3 rows in set (0.00 sec) mysql>
having和group by配合使用,对group by结果进行过滤
mysql> select deptno,avg(sal) from EMP group by deptno having avg(sal)<2000; +--------+-------------+ | deptno | avg(sal) | +--------+-------------+ | 30 | 1630.000000 | +--------+-------------+ 1 row in set (0.00 sec) mysql>
注意:having经常和group by搭配使用,作用是对分组进行筛选,作用有些像where,但有区别,不能混用!
mysql> select deptno,avg(sal) from EMP group by deptno where avg(sal)<2000; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where avg(sal)<2000' at line 1 mysql>
OKOK,MySQL的增删查改就到这里,如果你对Linux和C++也感兴趣的话,可以看看我的主页哦。下面是我的github主页,里面记录了我的学习代码和leetcode的一些题的题解,有兴趣的可以看看。