添加表数据==================================================
1、insert into 表名称 values(数据1,数据2,数据3,...)
2、insert into 表名称(字段1,字段2,字段3,...) values (数据1,数据2,数据3,...)
3、insert into 表名称 set 字段1 = 数据1,字段2 = 数据2,...
修改表数据==================================================
update 表名称 set 字段名 = 数据 where 定位字段 = 定位数据
删除表数据==================================================
delete from 表名称 where 字段名 = 数据
简单查询====================================================
1、基础查询
select * from 表名称
2、查询指定字段
select 字段1,字段2,字段3,... from 表名称
3、查询指定字段,并修改字段名(不会修改原表)
select 字段1 as 新字段名,字段2 as 新字段名 from 表名称
4、按条件查询
select * from 表名称 where 字段名 = 数据
5、多条件查询
select * from 表名称 where 字段1 = 数据 or 字段2 = 数据;
select * from 表名称 where 字段1 = 数据 and 字段2 = 数据;
6、范围查询,任何字段,数据为数字
select * from 表名称 where 字段名1 >= 小数值 and 字段名1 <= 大数值
select * from 表名称 where 字段名 between 数值 and 数值;
7、离散查询
select * from 表名称 where 字段1 = 数据1 or 字段1 = 数据2 or 字段1 = 数据3
select * from 表名称 where 字段名 in (数据1,数据2,数据3)
select * from 表名称 where 字段名 not in (数据1,数据2,数据3)
8、模糊查询
select * from 表名称 where 字段名 like '%数据_' (%可以为任意数据也可以没有数据,_必须为一个任意数据)
9、排序查询、asc 升序,desc 降序,order 秩序
select * from 表名称 order by 字段名 desc
select * from 表名称 order by 字段1 asc,字段2 desc(先通过字段1升序排列,相同部分再通过字段2降序排列)
10、去重查询 distinct 不同、区别
select distinct 字段名 from 表名称
11、分页查询 limit 限制
select * from 表名称 limit 跳过的数据数量,显示的数据数量
n;
m = 5;
limit(n-1)*m,m
12、聚合函数查询 count 总数量,sum 总和,avg 平均值
select count(*) from 表名称
select sum(数字型字段) from 表名称
select avg(数字型字段) from 表名称
select max(数字型字段) from 表名称
select min(数字型字段) from 表名称
高级查询============================================================================
一、分组查询
1、group by
select name,oil,powers from car group by powers
通过powers分组,显示name、oil、powers
select name,oil,powers from car group by powers,oil
通过powers和oil分组,显示name、oil、powers
2、group_concat(字段名)
select powers,group_concat(oil) from car group by powers
通过powers分组,分组显示所有的oil值
3、函数
select powers,count(*),max(price),min(price),avg(price) from car group by powers
通过powers分组,显示每组的数据数量、最大值、最小值、平均值
4、with rollup
select powers,count(*),max(price),min(price),avg(price) from car group by powers with rollup
通过powers分组,显示每组的数据数量、每组的最大值、每组的最小值、每组的平均值,并新建一组数据显示所有组的数据量、所有组的最大值、所有组的最小值、所有组的平均值
5、having
select powers,count(*),max(price),min(price),avg(price) from car group by powers with rollup having count(*) >=2
通过powers分组,显示数据数量大于等于2的组的数据数量、最大值、最小值、平均值,并新建一组数据显示所有组的数据量、所有组的最大值、所有组的最小值、所有组的平均值
二、链接查询
1、内链接
select a.sid,sname,smajor,sage,ssex from studen as a,other_stu as b where a.sid = b.sid and ssex = 'boy' and a.sid = '2'
select a.sid,sname,smajor,sage,ssex from studen as a inner join other_stu as b where a.sid = b.sid and ssex = 'boy' and a.sid = '2'
合并student表和other_stu表,显示ssex等于boy、sid等于2的sid、same、smajor、sage、ssex数据
2、左链接
select a.sid,sname,smajor,sage,ssex from studen as a left join other_stu as b on a.sid = b.sid and ssex = 'boy' and a.sid = '2'
3、右链接
select a.sid,sname,smajor,sage,ssex from studen as a right join other_stu as b on a.sid = b.sid and ssex = 'boy' and a.sid = '2'
4、自然链接
select a.sID,sName,sMajor,sAge,sSex from studen as a natural join other_stu as b;
select * from studen as a natural join other_stu as b;
自动清除笛卡儿积,把重复的字段合并
5、交叉链接
select * from studen as a cross join other_stu as b;
形成笛卡儿积
三、子查询
select * from studen as a left join (select * from other_stu where ssex = 'boy') as b on a.sid = b.sid and a.smajor = 'cs'
any/some
select * from studen as a where sid = some(select sid from other_stu where ssex = 'boy')
exists(子查询查到数据则执行外层查询,子查询没有查到数据则不执行外层查询)
select * from stude
select * from studen as a natural join other_stu as b where exists(select * from other_stu where ssex = 'boy') and b.ssex = 'boy'
四、联合查询:查询时,查询的字段个数要一样
union all
select * from studen union all select * from other_stu
select * from studen union all select * from studen
把两张表拼在一起,没有去重效果
union
select * from studen union select * from studen
两张表联合查询时,如果两个字段的所有数据都相同(字段类型可以不同)会把两张表拼在一起并去重