表查询常用关键字、多表查询

211 阅读9分钟

操作表的SQL语句补充

1.修改表名
    alter table 旧表名 rename 新表名;
2.新增字段
    alter table 表名 add 字段名 字段类型(数字) 约束条件;
    alter table 表名 add 字段名 字段类型(数字) 约束条件 after 已经存在的字段;  # 将字段新增在某个已存在的字段后面(控制新增字段位置)
    alter table 表名 add 字段名 字段类型(数字) 约束条件 first;
    # 将新字段添加至开头
3.修改字段
    alter table 表名 change 旧字段 新字段 字段类型 约束条件;
    alter table 表名 modify 字段名 新的字段类型 约束条件;
4.删除字段
    alter table 表名 drop 字段名;

表查询关键字

数据准备

1.数据准备(直接拷贝)
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);

表查询常见运算符

1.查询条件可以使用与或非(and or not) 使用方法与python语法一致。

2.可以使用成员运算 in  与  not in  使用方法与python语法一致。

3.模糊查询 like    通配符   % 与 _ 
    “%”通配符可以匹配 0 到多个任意字符。
    通配符的功能与“%”类似,其仅匹配任意一个字符。如需匹配两个字符,则使用“_ _”。

4. between ... and ...运算符
    选取介于两者之间的数据值,包括边界

5. is
    查询空值时不能使用 xxx=null , 而应该使用 xxx is null。

select 与 from

select:自定义查询表中字段对应的数据
from:指定操作的对象(一张或多张表)
'
补充:
1.SQL语句的关键字编写顺序与执行顺序是不一样的!
    eg:select name from emp;
    执行顺序:先执行from emp 确定在emp表中操作,在执行select name 确定要查询的字段,开始查询。
	
2.编写SQl时针对select 和 from 可以先写个模板之后再根据实际情况填充信息。:select * from 表名 其他操作

3.select 后面的字段可能是实际的 也可能是通过SQL动态产生的(实际不存在) 所以可以用*占位 最后再修改。
'

where 筛选

# 1.查询id大于等于3小于等于6的数据
select *from emp where id >=3 and id <=6;
select * from emp where id between 3 and 6;
# 2.查询薪资是20000或者18000或者17000的数据
select * from emp where salary = 20000 or salary = 18000 or salary = 17000;
select * from emp where salary in (17000,18000,20000);
# 3.查询员工姓名中包含o字母的员工姓名和薪资
select name,salary from emp where name like '%o%';
# 4.查询员工姓名是由四个字符组成的员工姓名与其薪资
select name,salary from emp where name like '____';
select name,salary from emp where char_length(name) = 4;
# 5.查询id小于3或者大于6的数据
select * from emp where id<3 or id>6;
select * from emp where id not between 3 and 6;
# 6.查询薪资不在20000,18000,17000范围的数据
select * from emp where salary not in (17000,18000,20000);
# 7.查询岗位描述为空的员工名与岗位名
select name,post from emp where post_comment = NULL;  # ×查询为空!  查询空值不能用 = ,只能用is
select name,post from emp where post_comment is NULL;

group by 分组

分组:按照一些指定的条件将单个的数据分为一个整体。

1. 分组之后我们的研究对象应该是以组为单位,不应该直接获取单个数据项,如果获取了应该直接报错。
2. select 后面可以直接写的字段名只能是分组的字段(其他字段应该需要借助于一些聚合函数才能获取)
eg:select post from emp group by post;
3.一般题目有这些字眼,就可以使用分组:
	每个、平均、最大、最小...
4.常见的配合分组使用的聚合函数
	max		最大值
	min		最小值
	sum		总和
	count	计数
	avg		平均

# 1.获取每个部门的最高工资  
# 思路:以组为单位统计组内数据>>>聚合查询(聚集到一起成为一个结果)
select post,max(salary) from emp group by post;
# 2.每个部门的工资总和
select post,sum(salary) from emp group by post;
# 3.每个部门的人数
select count(id) from emp group by post;
# 4.查询分组之后的部门名称和每个部门下所有的学生姓名
select post,group_concat(name) from emp group by post;

说明: grop_concat:分组之后使用,不仅可以显示除分组外的字段,还可以用来拼接字符串:
select post,group_concat(name,"DSB") from emp group by post;
select post,group_concat(name,"|",salary) from emp group by post;

having 过滤

where 与 having 的功能其实是一样的,都可以用来筛选数据
where用于分组之前的筛选,having用于分组之后的筛选
所以总的顺序是: Where > Group by > Having

# 统计各部门年龄在30岁以上的员工平均工资,并且保留平均工资大于10000的部门
select post,avg(salary) from emp
    where age >= 30
    group by post
    having avg(salary) > 10000;

distinct 去重

在对字段进行去重的时候,要保证distinct在所有字段的最前面
如果distinct关键字后面有多个字段时,则会对多个字段进行组合去重,只有多个字段组合起来的值是相等的才会被去重。
select distinct age from emp;

order by 排序

Where > Group by > Having > Order by

select * from emp order by salary asc;  # 默认升序
select * from emp order by salary 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 分页

1.限制展示条数
select * from emp limit 3;

# 查询工资最高的人的详细信息
思路:先将工资由高到低排序,再展示出第一个
select * from emp order by salary desc limit 1;

2.分页显示
select * from emp limit 0,5;  # 第一个参数表示起始位置,第二个参数表示的是条数,不是索引位置
select * from emp limit 5,10;

regexp 正则

select * from emp where name regexp '^j.*(n|y)$';

多表查询思路

子查询:将一张表的查询结果括起来当做另一条SQL语句的条件
连表操作:先将所有涉及到结果的表全部拼接到一起形成一张大表,然后从大表中查询数据。


#建表
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的部门名称
1.先获取jason的部门编号
	select dep_id from emp1 where name = 'jason';  # 200
2.根据部门编号获取部门名称
    select name from dep1 where id = 200;
 子查询
    select name from dep1 where id = (
    select dep_id from emp1 where name = 'jason');

连表操作

select * from emp1,dep1;  # 笛卡尔积
'''我们不会使用笛卡尔积来求数据 效率太低  连表有专门的语法'''

inner join		内连接
    只拼接两边都有的字段数据
left join		左连接
    以左表为基准 展示所有的数据 没有对应则NULL填充
right join		右连接
    以右表为基准 展示所有的数据 没有对应则NULL填充
union		   全连接
    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;

练习

-- 1. 查询岗位名以及岗位包含的所有员工名字
select post,GROUP_CONCAT(name) as '名字集合' from emp group by post;
-- 2. 查询岗位名以及各岗位内包含的员工个数
select post,count(id) as '个数' from emp group by post;
-- 3. 查询公司内男员工和女员工的个数
select gender,count(gender) as '个数' from emp group by gender;
-- 4. 查询岗位名以及各岗位的平均薪资
select post,avg(salary) as '平均薪资' from emp GROUP BY post;
-- 5. 查询岗位名以及各岗位的最高薪资
select post,max(salary) as '最高薪资' from emp group by post;
-- 6. 查询岗位名以及各岗位的最低薪资
select post,min(salary) as '最低薪资' from emp group by post;
-- 7. 查询男员工与男员工的平均薪资,女员工与女员工的平均薪资
select gender,avg(salary) as '平均薪资' from emp group by gender;
-- 8. 统计各部门年龄在30岁以上的员工平均工资
select post,avg(salary) as '平均薪资' from emp where age > 30 group by post;
-- 9. 统计各部门年龄在10岁以上的员工平均工资,并且保留平均工资大于1000的部门,然后对平均工资进行排序
select post,avg(salary) as '平均薪资' from emp WHERE age > 30 GROUP BY post
having avg(salary) > 1000 ORDER BY avg(salary);