//指定列插入
insert into table_name (c2,c3) values (v2,v3);
//多行插入
insert into table_name (c2,c3) values (v2,v3),(a2,a3)...;
//全列插入
insert into table_name values(v1,v2,...vn);
其中,into是可以省略的
insert into table_name (c2,c3) values (v2,v3) on duplicate key update c2=v2,c3=v3;
0 row affected : 有冲突,但冲突和更新的值是一样的
1 row affected : 没有冲突,直接插入了
2 row affected : 有冲突,且冲突数据已经更新
replace into table_name (sn,name) values(2001,'张飞');
- 主键/唯一键 有冲突删除后插入
- 主键/唯一键 没有冲突直接插入
//全列查询
select * from table_name;
//按列查询
select c1,c2,c3 from table_name;
//运算并命名
seect name,chinese+math+english as [name] from table_name;
as 可以带,也可以不带
运算符
运算符 | 说明 |
---|---|
= | 等于,对NULL不安全,例如NULL=NULL 结果是 NULL |
<=> | 等于,对NULL安全,例如NULL <=> NULL 结果是 true |
!=,<> | 不等于 |
BETWEEN a0 and b0 | 范围匹配,[a0,b0] |
IN (option,…) | 如果是option的任意一个,返回true |
IS NULL | 是NULL |
IS NOT NULL | 不是NULL |
LIKE | 模糊匹配,%表示多个任意字符,_表示任意一个字符 |
逻辑运算符:
AND | 所有为true才为true |
OR | 有一个为true就是true |
NOT | 条件为true,结果是false |
select name,english from score where english < 60;
select name,chinese from score where chinese between 80 and 90;
select name,math form score where math in (58,59,98,99);
select name from score where name like '孙%';
select name from score where name like '孙_';
select name,chinese,english from score where chinese > english;
select name,chinese+english+math as total from score where chinese+english+math < 200;
//执行顺序
//1、先执行from
//2、再按照筛选条件
//3、显示筛选结果
select name,chinese from score where chinese > 80 and name not like '孙%';
select name,chinese,math,english,chinese+math+english as total from score where (chinese+math+english > 200 and chinese < math and english > 80) OR name like '孙%';
语法:
select ... from table_name where ... ORDER BY [] [ASC|DESC]
说明:
ASC 为升序
DESC为降序
默认升序
select name,math from score ORDER BY math ASC;
select name,math,chinese,english from score ORDER BY math DESC, english ASC,chinese ASC;
select name,math+chinese+english as total from score order by total;
说明:先有数据才能排序,先进行筛选,再order by,可以使用total
select name,math from score where name = '孙%' or name = '曹%' order by math desc;
语法:
//从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;
语法:
对查询到的结果进行列值更新,没有where条件,则进行全表更新。
update table_name SET column1 = value1 [筛选条件];
update score set math=60 , chinese=70 where name = '小明';
//查:
select name,chinese+math+english as total from score order by total desc limit 0,3;
//
update score set math = math+30 order by chinese+math+english asc limit 3;
语法:
delete from table_name [where ...] [order by ...] [limit ...]
TRUNCATE [TABLE] table_name;
两种方式都可以清空表,前者不会影响AUTO_INCREMENT值,后者会将其置为初始值。
前者会包装成事物,而后者则直接运行,不会变成事物。
//创建一个结构一模一样的表
create table noduplicate_table like duplicate_table;
//查询去重
select distinct * from duplicate_table;
//插入查询到的数据
insert into no_duplicate [select distinct * from duplicate_table];
//
rename .. to ..;
重命名的方式:等一切就绪后,统一放入,更新,生效等。
函数 | 说明 |
---|---|
COUNT | 返回查询到数据的 数量 |
SUM | 返回查询到的数据的总和 , 不是数字无意义 |
AVG | 返回平均值,非数字无意义 |
MAX | 返回最大值,非数字无意义 |
MIN | 返回最小值,非数字无意义 |
//先对math去重 , 而不是对统计出来的数据去重
select COUNT(distinct math) as res from score;
分组目的:分组后聚合统计
分组的本质即把一张大表拆分成多个组,进行各自的统计分组,也可以认为 “分表” ,在逻辑上拆成多个子表。
select column1,column2, .. from table_name group by column3;
source [path]
select deptno, max(sal) 最高 , avg(sal) 平均 from emp group by deptno;
select deptno ,job, avg(sal) 平均 , min(sal) 最低 from emp group by deptno , job order by deptno asc;
1、统计出来每一个部门的平均工资
2、对聚合的结构进行判断
select deptno,avg(sal) 平均工资 from emp group by deptno having avg < 2000;
having:对聚合后的数据进行条件筛选。
where子句:对具体的任意列进行条件筛选
having子句:对分组聚合之后的结果进行条件筛选
1、先确认从哪个表里拿数据
2、where子句筛选条件
3、分组
4、按照分组后的数据进行聚合统计,重命名
5、对分组聚合后的结果进行条件筛选(having)
表不只是磁盘内的真实物理结构,中间筛选的,分出来的组,都可以理解成逻辑表。