MySQL数据库基础之数据查询学习
一.单表查询

1.创建表
1 create table emp( 2 id int not null unique auto_increment, 3 name varchar(20) not null, 4 sex enum('male','female') not null default 'male', 5 #大部分是男的 6 age int(3) unsigned not null default 28, 7 hire_date date not null, 8 post varchar(50), 9 post_comment varchar(100), 10 salary double(15,2), 11 office int, #一个部门一个屋子 12 depart_id int 13 );
2.插入记录
1 #三个部门:教学,销售,运营 2 insert into 3 emp(name,sex,age,hire_date,post,salary,office,depart_id) values 4 ('jason','male',18,'20170301','张江第一帅形象代言',7300.33,401,1), 5 #以下是教学部 6 ('egon','male',78,'20150302','teacher',1000000.31,401,1), 7 ('kevin','male',81,'20130305','teacher',8300,401,1), 8 ('tank','male',73,'20140701','teacher',3500,401,1), 9 ('owen','male',28,'20121101','teacher',2100,401,1), 10 ('jerry','female',18,'20110211','teacher',9000,401,1), 11 ('nick','male',18,'19000301','teacher',30000,401,1), 12 ('sean','male',48,'20101111','teacher',10000,401,1), 13 ('歪歪','female',48,'20150311','sale',3000.13,402,2),#以下是销售部门 14 ('丫丫','female',38,'20101101','sale',2000.35,402,2), 15 ('丁丁','female',18,'20110312','sale',1000.37,402,2), 16 ('星星','female',18,'20160513','sale',3000.29,402,2), 17 ('格格','female',28,'20170127','sale',4000.33,402,2), 18 ('张野','male',28,'20160311','operation',10000.13,403,3), 19 #以下是运营部门 20 ('程咬金','male',18,'19970312','operation',20000,403,3), 21 ('程咬银','female',18,'20130311','operation',19000,403,3), 22 ('程咬铜','male',18,'20150411','operation',18000,403,3), 23 ('程咬铁','female',18,'20140512','operation',17000,403,3) 24 ; 25 #ps:如果在windows系统中,插入中文字符,select的结果为空白,可以将所有字符编码统一设置成gbk
3.where约束条件
# 1.查询id大于等于3小于等于6的数据
1 select id,name from emp where id >= 3 and id <= 6; 2 select * from emp where id between 3 and 6;
# 2.查询薪资是20000或者18000或者17000的数据
1 select * from emp where salary = 20000 or salary = 18000 or salary = 17000; 2 select * from emp where salary in 3 (20000,18000,17000); # 简写
# 3.查询员工姓名中包含o字母的员工姓名和薪资
# 在你刚开始接触mysql查询的时候,建议你按照查询的优先级顺序拼写出你的sql语句
先是查哪张表 from emp
再是根据什么条件去查 where name like ‘%o%’
再是对查询出来的数据筛选展示部分 select name,salary
select name,salary from emp where name like '%o%';
补:# like: 模糊匹配 # %: 匹配0个或多个任意字符 # _: 匹配一个任意字符
# 4.查询员工姓名是由四个字符组成的员工姓名与其薪资
1 select name,salary from emp where name like '____'; 2 select name,salary from emp where char_length(name) 3 = 4;
# 5.查询id小于3或者大于6的数据
1 select * from emp where id not between 3 and 6;
# 6.查询薪资不在20000,18000,17000范围的数据
1 select * from emp where salary not in (20000,18000,17000);
# 7.查询岗位描述为空的员工名与岗位名 针对null不能用等号,只能用is
1 select name,post from emp where post_comment = NULL; 2 # 查询为空! 3 select name,post from emp where post_comment is 4 NULL; 5 select name,post from emp where post_comment is not 6 NULL;
4.groud by
1 SELECT sex FROM users GROUP BY sex(列名);
在这里,因为sex只有0和null两种值,所以被分做两组
#1.以组为单位统计组内数据>>>聚合查询(聚集到一起合成为一个结果)
# 每个部门的最高工资
1 select post,max(salary) from emp group by post;
# 每个部门的最低工资
1 select post,min(salary) from emp group by post;
# 每个部门的工资总和
1 select post,sum(salary) from emp group by post;
# 每个部门的人数
1 select post,count(id) from emp group by post;
#2.查询分组之后的部门名称和每个部门下所有的学生姓名
# group_concat(分组之后用)不仅可以用来显示除分组外字段还有拼接字符串的作用
1 select post,group_concat(name) from emp group by 2 post; 3 select post,group_concat(name,"_SB") from 4 emp group by post; 5 select post,group_concat(name,": ",salary) 6 from emp group by post; 7 select post,group_concat(salary) from emp group by 8 post;
补:查询岗位名以及岗位包含的所有员工名字?
1 select post,group_concat(name) from emp group by post;
#3.补充concat(不分组时用)拼接字符串达到更好的显示效果 as语法使用
1 select name as 姓名,salary as 薪资 from emp; 2 select concat("NAME: ",name) as 3 姓名,concat("SAL: ",salary) as 薪资 from emp;
# 补充as语法 即可以给字段起别名也可以给表起
1 select emp.id,emp.name from emp as t1; # 报错 因为表名已经被你改成了t1 2 select t1.id,t1.name from emp as t1;
#4. 查询四则运算
# 查询每个人的年薪
1 select name,salary*12 as annual_salary from emp; 2 select name,salary*12 annual_salary from emp; 3 # as可以省略
5.where与groud by
统计各部门年龄在30岁以上的员工平均工资
1 select post,avg(salary) from emp where age > 30 2 group by post;
# 对where过滤出来的虚拟表进行一个分组
6.having句型
having的语法格式与where一致,只不过having是在分组之后进行的过滤,即where虽然不能用聚合函数,但是having可以!
统计各部门年龄在30岁以上的员工平均工资,并且保留平均工资大于10000的部门
1 select post,avg(salary) from emp 2 where age >= 30 3 group by post 4 having avg(salary) > 5 10000;
7.distinct
对有重复的展示数据进行去重操作
1 select distinct post from emp;
8.order by
1 select * from emp order by salary asc; #默认升序排 2 select * from emp order by salary desc; #降序排
9.limit
先降序排列,再从最后往前数取两位。
1 SELECT 2 *FROM users ORDER BY id DESC LIMIT 2,2;
# 分页显示
1 select * from emp limit 0,5; 2 第一个参数表示起始位置,第二个参数表示的是条数,不是索引位置
# 限制展示条数
1 select * from emp limit 3;
# 查询工资最高的人的详细信息
1 select * from emp order by salary desc limit 1; 2 SELECT *FROM users LIMIT 2;从第一条开始返回,返回两条记录。 3 2,2;从第三条返回,返回两条。
10.聚合函数
count: 计数 max: 最大值 min: 最小值 avg: 平均值 sum: 求和
注意: 聚合函数:
- 只能跟在group by后(执行顺序)使用
- 若查询语句没有group by,则默认整张表就是一个分组。
11.正则
在编程中,凡是看到reg开头的,基本上都是跟正则有关
1 select * from emp where name regexp '^程.*(金|银|铜|铁)$';
12.其他
1 select post 2 from emp group by post; # 获取部门信息
13.顺序
书写顺序: - select - distinct -from- where - group by - having - order by - limit
执行顺序: -from- where - group by - having - select - distinct - order by - limit
二.多表查询
1.建表
1 create table dep( 2 id int, 3 name varchar(20) 4 ); 5 create table emp( 6 id int primary key auto_increment, 7 name varchar(20), 8 sex enum('male','female') not null default 'male', 9 age int, 10 dep_id int 11 );
2.插入数据
1 insert into dep values 2 (200,'技术'), 3 (201,'人力资源'), 4 (202,'销售'), 5 (203,'运营'); 6 insert into emp(name,sex,age,dep_id) values 7 ('jason','male',18,200), 8 ('egon','female',48,201), 9 ('kevin','male',38,201), 10 ('nick','female',28,202), 11 ('owen','male',18,200), 12 ('jerry','female',18,204) 13 ;
3.表查询
1 select * from emp,dep; 2 #左表一条记录与右表所有记录都对应一遍>>>笛卡尔积 3 # 查询员工及所在部门的信息 4 select * from emp,dep where emp.dep_id = dep.id; 5 # 查询部门为技术部的员工及部门信息 6 select * from emp,dep where emp.dep_id = dep.id and 7 dep.name = '技术';
4. 将两张表关联到一起的操作,有专门对应的方法
1 # 1、内连接:只取两张表有对应关系的记录 2 select * from emp inner join dep 3 on emp.dep_id = dep.id; 4 select * from emp inner join dep on emp.dep_id = 5 dep.id 6 where dep.name ="技术"; 7 # 2、左连接: 在内连接的基础上保留左表没有对应关系的记录 8 select * from emp left join dep 9 on emp.dep_id = dep.id; 10 # 3、右连接: 在内连接的基础上保留右表没有对应关系的记录 11 select * from emp right join dep 12 on emp.dep_id = dep.id; 13 # 4、全连接:在内连接的基础上保留左、右面表没有对应关系的的记录 14 select * from emp left join dep 15 on emp.dep_id = dep.id 16 union 17 select * from emp right join dep 18 on emp.dep_id = dep.id;
5.子查询
# 1.查询部门是技术或者人力资源的员工信息 select * from emp where dep_id in (select id from dep where name = "技术" or name = "人力资源");

浙公网安备 33010602011771号