·新增数据
·多数据插入
insert into 表名 [(字段列表)] values (值),(值),(值);
·主键冲突
create table my_student(
stu_id varchar(10) primary key,
stu_name varchar(10) not null
);
insert into my_student values('stu0001','wkm'),('stu0002','zqy');
insert into my_student values('stu0001','james'); --冲突
1.主键冲突更新:采用更新方法
insert into 表名[(字段列表)] values(值) on duplicate key update 字段 = 新值
-- 实例
insert into my_student values('stu0001','james')
on duplicate key update name = 'james'; --wkm被替换成james
2.主键冲突替换
replace into 表名 [(字段列表)] values (值) --insert效率更高,两行受影响
·蠕虫复制
一分为二,成倍增加,从已有数据中获取到数据并插入到数据表
insert into 表名 [(字段列表)] select 字段列表/* from 表名
- 蠕虫复制通常是无业务意义的重复数据,可以短期内快速增加表的数据量,从而用来测试表的压力,还可以通过大量数据来测试表的效率。
- 要注意主键冲突
·更新数据
1.注意通常一定是跟随条件更新
update 表名 set 字段名 = 新值 where 条件
2.如果没有条件是全表更新,但是可以用limit来限制更新的数量
update 表名 set 字段名 = 新值 [where 条件] limit 数量
·删除数据
同上
·查询数据
完整指令
select select选项 字段列表/* from 数据源 where 条件 group by 分组 having 条件 order by 排序 limit 限制;
·select 选项
系统如何对待查询得到的结果
- all:默认值,保存所有的纪录
- distinct:去重,去除重复(所有字段)的记录,只保留一条
·字段列表
从多张表获取数据时可能不同表有同名字段,需要重新命名
字段名 [as] 别名
--实例
select name name1, name as name2 FROM my_teacher;
·from 数据源
数据源只要是一个符合二维表结构的数据即可
1. 单表数据
from 表名
2. 多表数据
from 表1,表2...
select * from 表1,表2; --结果:两张表的记录数相乘,字段数拼接。对数据库会造成压力
3. 动态数据
数据源是表中的查询结果
from (select 字段列表 from 表名) [as] 别名 --别名不显示
·where 条件
从数据表获取数据的时候进行条件筛选,通过运算符实现
·group by 分组
根据指定字段将数据进行分组,为了统计
1. 分组统计
group by 字段名
--实例
create table my_teacher(
name varchar(10) not null,
class int not null
);
insert into my_teacher values('张三',1),('李四',1),('王五',2),('赵六',2);
select * from my_teacher group by class;
group by 是为了分组后进行数据统计的,如果想看数据显示,group by无用
group by 分组后只保留每组第一条记录
可以利用一些统计函数(聚合函数):
- count():统计每组中的数量,如果目标是字段,不统计为NULL的字段,如果是*是统计记录
- avg():求平均值
- sum():求和
- max():求最大值
- min():求最小值
create table my_student(
stu_id varchar(10) not null,
stu_name varchar(10) not null,
class_id tinyint unsigned not null,
stu_age tinyint unsigned not null,
stu_height tinyint unsigned not null
);
insert into my_student values
('stu001','wkm',1,21,170),('stu002','zqy',1,19,160),
('stu003','james',2,18,189),('stu004','tom',2,23,175);
-- 利用聚合函数筛选
select class_id,count(*),max(stu_age),min(stu_height),avg(stu_age)from my_student group by class_id;
group_concat():为了将分组中指定的字段进行合并(字符串拼接)
select group_concat(stu_name) from my_student group by class_id;
2. 多分组
按照字段分组之后,对已经分组的数据再次进行分组
group by 字段1,字段2 --先按照字段1分组排序(首字母),再把结果按照字段2分组排序
--实例
alter table my_student add gender enum ('男','女');
update my_student set gender = 1 where stu_id in ('stu001','stu004','stu003');
update my_student set gender = 2 where stu_id = 'stu002';
insert into my_student values ('stu005','sally',2,20,165,2),
('stu006','jack',3,25,180,1),('stu007','rose',3,23,165,2);
select class_id,count(*),gender,group_concat(stu_name)
from my_student group by class_id,gender;
3. 分组排序
分组默认排序,按照字段排序
group by 字段1[asc|desc],字段2[asc|desc]..... --默认asc
4. 回溯统计
当分组进行多分组之后,往上统计的过程中,需要进行层层上报,将这种层层上报统计的过程称之为回溯统计。每一次分组向上统计的过程都会产生一次新的数据统计,而且当前数据对应的分组字段为null
group by 字段 [asc|desc] with rollup;
select class_id, count(*) from my_student group by class_id with rollup;
select class_id, gender, count(*)
from my_student group by class_id,gender with rollup;
·having 条件
本质和where一样,区别:
having 在group by之后,可以对分组数据筛选,where不能用聚合函数,聚合函数是用在group by分组之后的,where已经运行完毕,只能用having来解决(使用聚合函数或字段别名)。
select class_id,count(*) as number from my_student group by class_id having number > 2;
where表示把从磁盘拿到内存,where之后的所有操作都是内存操作。
·order by 排序
order by 字段名 [asc|desc] --默认是asc
--实例
select * from my_student order by stu_height desc;
order by 也可以像 group by 一样进行多排序
·limit 限制
1. 记录数限制
纯粹的限制获取的数量,从第一条到指定数量
limit 数量
limit通常在限定为一条记录的时候使用的比较多,有时获取多条记录并不能解决业务问题,但是会增加服务器压力
2.分页
用limit来限制获取指定区间的数据
limit offset,length; --偏移量,具体获取条数
select * from my_student limit 0,2; --获取前两条信息