增 insert

增加一条记录

  1. insert tb set col_1='a',col_2=10;

增加多条记录

需完全对应

  1. insert tb values(...,...);

无需完全对应

  1. insert tb (col_1,col_2) values(...,...);

增加查询来的记录(蠕虫复制)

  1. insert tb set [col_1] select ...;

向tb插入tb1的数据

  1. insert tb select * from tb1 [where 条件];

向tb插入tb1的数据(可选择插入列)

  1. insert tb(col_1,col_2) select col_1,col_2 from tb1 [where 条件];

增加编号+10的记录

  1. insert tb_1 (select max(deptno)+10,"AAA","SSS" from tb_2);

增加无错误记录

忽略错误,即错误值不插入

  1. insert ignore tb ...;

删 delete

删除单表记录

删除20部门工资最高的五人记录

  1. delete from tb
  2. where deptno=20
  3. order by sal
  4. limit 5;

删除多表记录

删除dept表中的sales部门和emp表中sales部门的所有记录

  1. delete e,d
  2. from emp e join dept d
  3. on e.deptno=d.deptno
  4. where d.dname="sales";

删除列中的指定行

  1. delete from tb where col_2='...';

删除全部记录

重新创建个空表

  1. truncate table tb;

一条条删除

  1. delete table tb;

删除重复数据

删除重复邮箱中Id小的数据

  1. delete p1 from Person p1,Person p2
  2. where p1.Email = p2.Email and p1.Id > p2.Id

连接子句:重复姓名的最大id的数据 删除逻辑:删除重复姓名中id比子句中id小的数据

  1. delete a from tb a join (
  2. select name,count(*),max(id) as id from tb
  3. group by name having count(*)>1) b
  4. on a.name=b.name
  5. where a.id<b.id

改 update

更新单表记录

  1. update tb set col_1=公式/值,col_2=公式/值 [where 条件];

更新多表记录

底薪低于平均的加100元

  1. update t_emp e
  2. join (select avg(sal) avg from t_emp) t
  3. on e.sal<t.avg
  4. set sal=sal+100;

查 select

子句查询执行顺序:from-where-group by-having-select-order by-limit

查看数据

  1. select * from tb;

查看指定列数据

  1. select col_1 c1,col_2/10 c2 from tb;

条件查询 where

where分组前过滤,having分组后过滤

  1. select col_1 from tb where 条件 and/or 条件;

多列筛选

  1. where (col_1,col_2) in (select col_1,col_2 from tb1)

薪水在4000到6000间

  1. salary between 4000 and 6000

模糊查询:第二个字母是J开头(_:表示一个字符;%:表示任意个字符)

  1. name like "_j%"

指定部门内

  1. deptno in (10,20,30)

正则表达式(匹配中文名)

  1. name regexp '^[\\u4e00-\\u9fa5]{2,4}$'

聚合 group by

整列聚合 count / sum / avg / max / min

  1. select sum(col) from tb;

一共多少行 count(*)

  1. select count(*) from tb;

仅含每组第一条数据(不常用)

  1. select * from tb group by col_1,col_2 [asc/desc];

where分组前过滤,having分组后过滤(常用)

  1. select sex , type , sum(col) from tb [where 条件] group by sex , type [having 条件];

聚合集合 group_concat

  1. select col_1 group_concat(col_2) from...group by col_1;

给各聚合分类再汇总 rollup

  1. select...from...group by...with rollup;

排序 order by

asc(默认升序)/desc(降序)

  1. select * from tb order by col_1 desc,col_2 desc [having 条件];

分页 limit

起始位置,偏移量

  1. select * from tb limit 0,20;

忽略 offset

  1. select * from tb limit 1 offset 1;

去重 distinct

多字段的组合去重

  1. select distinct col_1,col_2 from tb;

有重复的姓名

  1. select name,count(*) from tb group by name having count(*)>1;

null转换为0

  1. ifnull(col_1,0)

合并 union

字段需一一对应 union:合并结果去重;union all:合并结果不去重;

  1. select a.name,a.age from tb1 a union [all] select b.name,b.age from tb2 b;

行转列

多行 转 多列

  1. select sum(case when sex='male' then numbers end) as 'male',
  2. sum(case when sex='female' then numbers end) as 'female' from tb;

连接

连接条件

e.deptno=d.deptno # 两表的相同字段
e.sal between s.low and s.high # 表1的字段在表2的两字段值之间

内连接 join

  1. select tb1.name,tb2.salary from tb1 join tb2 on tb1.name=tb2.name [where 条件];

左/右外连接 left/right join

显示左表全部和右表符合连接的记录 / 显示右表全部和左表符合连接的记录

  1. select * from tb1 left join tb2 on tb1.name=tb2.name;

外连接 outer join

  1. select * from tb1 outer join tb2 on tb1.name=tb2.name;

交叉连接 cross join

也称笛卡尔连接。两表相乘(行数 = tb1行数 * tb2行数)

  1. select * from tb1 cross join tb2;

子查询推荐在from子句中,不推荐在where子句中,因为会执行多次

自建表放在from子句中连接(大于平均薪水的员工)

  1. select e.ename
  2. from t_emp e join (select avg(sal) avg from t_emp) t
  3. on e.sal>t.avg;

子句和表连接的区别

子句(找出tb表中含有tb1表里的id的name,只要tb表内name不重复,就不会有重复)

  1. select name from tb where id in (select id from tb1);

表连接(tb1表的id可能会重复,因此需去重)

  1. select distinct name from tb a join tb1 b on a.id=b.id