sql语句补充
-
- 修改表名
alter table 表名 rename 新表名;
-
- 新增字段
alter table 表名 add 字段名 字段类型(数字) 约束条件;
alter table 表名 add 字段名 字段类型(数字) 约束条件 after 已经存在的字段;
alter table 表名 add 字段名 字段类型(数字) 约束条件 first;
-
- 修改字段
alter table 表名 change 旧字段 新字段 字段类型(数字) 约束条件;
alter table 表名 modify 字段名 新的字段类型(数字) 约束条件;
-
- 删除字段
alter table 表名 drop 字段名;
表查询关键字
所有操作基于以下两个表:
create table emp(
id int not null unique auto_increment,
name varchar(20) not null,
sex enum('male','female') not null default 'male', #大部分是男的
age int(3) unsigned not null default 28,
hire_date date not null,
post varchar(50),
post_comment varchar(100),
salary double(15,2),
office int, #一个部门一个屋子
depart_id int
);
#三个部门:教学,销售,运营
insert into emp(name,sex,age,hire_date,post,salary,office,depart_id) values
('jason','male',18,'20170301','浦东第一帅形象代言',7300.33,401,1), #以下是教学部
('tom','male',78,'20150302','teacher',1000000.31,401,1),
('kevin','male',81,'20130305','teacher',8300,401,1),
('tony','male',73,'20140701','teacher',3500,401,1),
('owen','male',28,'20121101','teacher',2100,401,1),
('jack','female',18,'20110211','teacher',9000,401,1),
('jenny','male',18,'19000301','teacher',30000,401,1),
('sank','male',48,'20101111','teacher',10000,401,1),
('哈哈','female',48,'20150311','sale',3000.13,402,2),#以下是销售部门
('呵呵','female',38,'20101101','sale',2000.35,402,2),
('西西','female',18,'20110312','sale',1000.37,402,2),
('乐乐','female',18,'20160513','sale',3000.29,402,2),
('拉拉','female',28,'20170127','sale',4000.33,402,2),
('僧龙','male',28,'20160311','operation',10000.13,403,3), #以下是运营部门
('程咬金','male',18,'19970312','operation',20000,403,3),
('程咬银','female',18,'20130311','operation',19000,403,3),
('程咬铜','male',18,'20150411','operation',18000,403,3),
('程咬铁','female',18,'20140512','operation',17000,403,3);
select与from
select name from emp;
select * from emp; # 所以可以先用*占位再修改
因为select后的字段有可能是表里实际有的,也可能是通过SQL动态产生的。
select * from 表名 其他操作 # 针对select与from的固定模板
先执行from确定表,才能执行select确定字段
where筛选
select * from 表名 where 字段条件; # *可以换成想要查询的字段名
例:
# 查询id大于等于3.小于等于6
select * from emp where id>=3 and id<=6;
select * from emp where id between 3 and 6;
# 查询id小于3,大于6的数据
select * from emp where id<3 or id>6;
select * from emp where id not between 3 and 6;
# 查询薪资是20000或者18000或者17000的数据
select * from emp where salary = 20000 or salary = 18000 or salary =17000;
select * from emp where salary in (20000,18000,17000);
# 查询薪资不在20000,18000,17000范围的数据
select * from emp where salary not in (20000,18000,17000);
# 查询员工姓名中包含o字母的员工姓名和薪资
select name,salary from emp where name like '%o%'
# 查询员工姓名是由四个字符组成的员工姓名与其薪资
select name,salary from emp where name like '____';
select name,salary from emp where char_length(name) = 4;
# 查询岗位描述为空的员工名与岗位名
select name,post from emp where post_comment is NULL; # 针对null不能用等号,只能用is
group by分组
# 获取每个部门的最高薪资 max 最大值
select post, max(salary) from emp group by post;
# 获取每个部门的最低薪资 min 最小值
select post, min(salary) from emp group by post;
# 获取每个部门的平均薪资 avg 平均
select post, avg(salary) from emp group by post;
# 获取每个部门的薪资总和 sum 总和
select post, sum(salary) from emp group by post;
# 获取每个部门下的总人数 count 计数
select post, count(id) from emp group by post;
# 查询分组之后的部门名称和每个部门下所有的学生姓名 group_concat
select post,group_concat(name) from emp group by post;
# 给显示的字段取别名
select post as '部门',max(salary) as '最高工资' from emp group by post;
# 给分组后的部门采用符号分隔 (name,": ",salary)按要求更改
select post,group_concat(name,": ",salary) from emp group by post;
having过滤
- where用于分组之前的筛选,而having用于分组之后的筛选
# 统计各部门年龄在30岁以上的员工平均工资,并且保留平均工资大于10000的部门
select post,avg(salary) from emp
where age >= 30
group by post
having avg(salary) > 10000
distinct去重
# 查询员工的所有年龄
select distinct age from emp;
# 查询员工的所有性别
select distinct sex from emp;
order by排序
# 对员工的薪资进行升序排列 asc
select * from emp order by salary asc; # asc也可以不写,因为默认升序
# 对员工的年龄进行升序排列
select * from emp order by age asc;
# 对员工的薪资进行降序排列 desc
select * from emp order by salary desc;
# 对员工的年龄进行降序排列
select * from emp order by age desc;
# 先按照age降序排,在年轻相同的情况下再按照薪资升序排
select * from emp order by age desc,salary asc;
# 统计各部门年龄在10岁以上的员工平均工资,保留平均工资大于1000的部门,然后对平均工资进行排序
select post,avg(salary) from emp where age > 10
group by post having avg(salary) > 1000 order by avg(salary);
limit分页
# 限制要展示的数据条数
select * from emp limit 3;
# 指定要展示的数据信息 0的位置是起始,5的位置是条数
select * from emp limit 0,5;
regexp正则
- 通过sql语句与正则表达式的结合筛选出符合条件的数据
# 展示名字以j或o开头,n或y结尾的数据
select * from emp where name regexp '^(j|o).*(n|y)$';
多表查询
此次多表查询基于下面两张表
create table dep1(
id int primary key auto_increment,
name varchar(20)
);
create table emp1(
id int primary key auto_increment,
name varchar(20),
gender enum('male','female') not null default 'male',
age int,
dep_id int
);
#插入数据
insert into dep1 values
(200,'技术'),
(201,'人力资源'),
(202,'销售'),
(203,'运营'),
(205,'安保')
;
insert into emp1(name,gender,age,dep_id) values
('jason','male',18,200),
('dragon','female',48,201),
('kevin','male',18,201),
('nick','male',28,202),
('owen','male',18,203),
('jerry','female',18,204);
子查询
- 基于一个表的结果进行二次操作,第一次的结果变为第二次的条件
# 查询jason所在部门编号的部门名称
select name from dep1 where id = (select dep_id from emp1 where name = 'jason');
连表操作
# 查询展示两边表都有的字段数据
select * from emp1 inner join dep1 on emp1.dep_id = dep1.id;
# 查询展示左表都有的字段数据
select * from emp1 left join dep1 on emp1.dep_id = dep1.id;
# 查询展示右表都有的字段数据
select * from emp1 right join dep1 on emp1.dep_id = dep1.id;
# 查询展示两边表所有的字段数据
select * from emp1 left join dep1 on emp1.dep_id = dep1.id
union
select * from emp1 right join dep1 on emp1.dep_id = dep1.id;
SQL小知识点补充
语法:
as 给字段起别名,给SQL语句取表名
comment 给表,字段添加注释信息
concat 用于分组之前多个字段数据的拼接
concat_ws 与concat作用一致,但是可以多个减少代码
exists exists后面的sql语句有结果就执行前面的sql语句,反之不执行
语法的用法:
comment:
create table server(id int) comment '这个server意思是服务器表'
create table t1(
id int comment '用户编号',
name varchar(16) comment '用户名'
) comment '用户表';
concat:
select concat(name,'|',pwd) from userinfo
select concat('|',id,name,pwd) from userinfo
exists:
select * from userinfo where exists (select * from department where id<100)