mysql-语法大全

DDL语句

创建

create database 库名 charset utf8;

删除

drop  database 库名;

修改

alter database 库名 charset latin;

创建

create table 表名(字段1 数据类型 [约束],字段2 数据类型 [约束],。。。);

删除

drop table 表名;

修改

#修改表名
alter table 表名 rename 新表名;

#修改字段
alter table 表名 modify 字段 数据类型 约束;

#修改字段并改名
alter table 表名 change 字段 新字段 数据类型 约束;

#删除字段
alter table 表名 drop 字段;

#添加字段
alter table 表名 add 字段 数据类型 约束;

DML语句

主要是表内容的增删改查

insert into 表名[(字段1,字段2.。。)] values(字段1,字段2.。。。),(字段1,字段2.。。。);

delete from 表名 where 条件

update from 表名 set 字段=值 where 条件;

条件顺序:from、where、group by、having(决定筛选出来的表格) select、distinct、order by、limit (决定显示出来的表格)

#查询所有
select * from 表名 where 条件;

#按字段排序
select * from 表名 order by 字段,字段1 

#限制查询
select * from 表名 order by 字段 limit 3; #查询3条(索引从0开始)

#分页查询
select * from 表名 limit 0,3;#查询0,1,2
select * from 表名 limit 3,3; #查询3,4,5

聚合

很多情况下,用户都需要进行一些汇总操作,比如统计整个公司对的人数或者统计每个部门的人数,这是就需要用到聚合操作
1、聚合函数sum()、count()、max()、min()等
2、分组group by通过某字段分组
3、having对分组后的结果在进行过滤(where在分组前过滤having在分组后)

select count(1) from 表名 #统计总行数

select  deptno ,count(deptno) from emp group by deptno ; #统计各部门的人数

select  deptno ,count(deptno) from emp group by deptno having count(deptno)>1 ; #统计人数大于1的部门

select  deptno ,group_concat(ename) from emp group by deptno #显示出各部门的人名

posted @ 2018-10-25 17:01  Kingfan  阅读(187)  评论(0编辑  收藏  举报