目录:
1、DDL:数据定义语言(data definition language)
2、DML:数据操作语言(Data Manipulation Language)
3、DCL:数据控制语言(Data Control Language)
1、DDL:数据定义语言(data definition language)
2、DML:数据操作语言(Data Manipulation Language)
查询记录语句
查询数据:select * from 表名 where 范围
总数:select count(*) from 表名 where 范围
select count(distinct(字段名)) from 表名 where 范围【distinct可以去重】
求和:select sum(字段名) from 表名 where 范围
平均:select avg(字段名) from 表名 where 范围
最大:select max(字段名) from 表名 where 范围
最小:select min(字段名) from 表名 where 范围
排序:select * from 表名 where 范围 order by 排序字段名 desc(desc降序排列;asc升序排列)
分组:根据某一个或多个列表字段进行分组统计。
查询每个用户的最高成绩:
select name,max(score) as max_score from table1 group by name
查询结果:先按用户名分组,再在每个组中查询找到最高分数
having的用法:同where用法,having与group by连用。where是筛选单个记录,having是筛选分组记录(先分组,后筛选)
插入记录语句
insert into 表名(字段1,字段2)values(字段1值,字段2值)
修改记录语句
update 表名 set 字段名=‘新值’ where 范围
删除记录语句
delete:delete from 表名 where 范围(删除表内符合条件的内容)
delete from 表名(清空数据表记录,不释放空间,即:下次插入数据,ID依然接着删除数据的ID继续增加)
truncate:truncate table 表名(清空数据表记录,释放空间,即:下次插入数据,ID从1重新开始)
drop:drop table 表名(删除表结构和记录)
PS:复杂查询
1、嵌套查询:多个查询语句嵌套在一起查询,一般嵌套的查询语句放在where 或 having 的后面
select * from table1 where status in(select status from table2)
2、多表连接查询
table1:
table2:
(1)内联查询(inner join……on……)
定义:仅将两个表中满足连接条件的行组合起来作为结果集
select * from table1 a inner join table2 b on a.id=b.id
查询结果:
(2)左外联(left outer join……on……)
定义:在内连接的基础上,还包含左表中所有不符合条件的数据行,并在其中的右表列填写NULL
select * from table1 a left outer join table2 b on a.id=b.id
查询结果:
(3)右外联(right outer join……on……)
定义:在内连接的基础上,还包含右表中所有不符合条件的数据行,并在其中的左表列填写NULL
select * from table1 a right outer join table2 b on a.id=b.id
查询结果:
(4)全外联(full outer join……on……)
定义:在内连接的基础上,还包含两个表中所有不符合条件的数据行,并在其中的左表、和右表列填写NULL
select * from table1 a full outer join table2 b on a.id=b.id
查询结果: