ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

MySQL之表查询关键字

2022-08-17 22:04:02  阅读:171  来源: 互联网

标签:salary name 之表 关键字 emp MySQL post id select


操作表的SQL语句补充

修改表名
创建一张表:
create table aa(id int primary key auto_increment);
alter table 表名 rename 新表名;
alter table aa rename abb;
新增字段
1.alter table 表名 add 字段名 字段类型(数字)约束条件;
  alter table abb add name varchar(32); 
2.alter table 表名 add 字段名 字段类型(数字)约束条件 after 已有的字段;
 alter table abb add pwd int not null after name;
3.alter table 表名 add 字段名 字段类型(数字) 约束条件 first;
 alter table abb add nid int first;

image

修改字段
1.alter table 表名 change 旧字段 新字段 字段类型(数字)约束条件;
  alter table abb change name username char(32) not null;
2.alter table 表名 modify 字段名 新的字段类型(数字)约束条件;
  alter table abb modify pwd bigint not null;

image

删除字段
alter table 表名 drop 字段名;
 alter table abb drop nid;

image

查询表关键字

数据准备(员工部门表)
	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);

image

select与from
sql语句的编写顺序与执行顺序是不一致的!
 eg:
    select * from emp
 肯定是先执行 emp 然后才能查找emp里的所有记录
 select 自定义查询表中字段的数据
 from 指定操作对象
where筛选
#1.查询id大于3且小于等于6的数据
  select id,name from emp where id>=3 and id<=6;  # 运行结果 图1
  select * from emp where id between 3 and 6; # 运行结果 图2

图1:
image
图2:
image

#2.查询薪资是20000或者18000或者17000的数据
 select * from emp where salary=20000 or salary=18000 or salary=17000; # 运行结果 图3
 select * from emp where salary in(20000,18000,17000); # 运行结果 图4

图3:
image
图4:
image

#3.查询员工姓名中包含字母o的员工姓名和薪资
 select name,salary from emp where name like '%o%'; # 运行结果 图5

图5:
image

#4.查询由四个字组成的员工姓名和薪资
 select name,salary from emp where name like '____'; #运行结果 图6
 select name,salary from emp where char_length(name)= 4; #运行结果 图7

图6;
image
图7:
image


#5.查询id小于3或者大于6的数据
 select * from emp where id<3 or id>6; # 运行结果 图8
 select * from emp where id not between 3 and 6;

图8:
image
image

#6.查询薪资不在20000,18000,17000的员工数据
 select * from emp where salary not in(20000,18000,17000) # 运行结果 图9

图9:
image

#7.查询岗位描述为空的员工名和岗位名 
 select name,post from emp where post_comment is null; #运行结果 图10
 select name,post from emp where post_comment is not null;

图10:
image

group by分组
1.分组:按照指定的条件将单个单个的数据分为一个个整体
  分组以后我们研究的对象应该是以组为单位 不可以直接获取单个数据项 select后面可以直接填写的字段名只能是分组的依据 
  分组后获取的字段名需要在配置文件加上 set global sql_mode='strict_trans_tables,only_full_group_by';
  也可以临时绑定 放在MySQL服务端
  
2.分组常见的一些有聚合函数:
   max min sum count avg 
ps:我们在写sql语句的时候 如果出现了每个,平均,最大/小,求和等 是需要用到分组的
1.获取每个部门的最高工资
 思路:以组为单位统计每个部门的数据
 select post,max(salary) from emp group by post;
 # 可以把要统计的字段更改名字 更直观
 select post as '部门', max(salary) as '最高工资' emp group by post;

image
image

2.获取每个部门的最低工资
select post,min(salary) from emp group by post;
3.获取每个部门的平均工资
select post,avg(salary) from emp group by post;
4.获取各个部门工资的总和
select post,sum(salary) from emp group by post;
5.获取每个部门的人数
select post,count(id) from emp groud by post;
6.查询分组后的每个部门和每个部门下员工的姓名
# group_concat 用于获取分组以外的字段数据 还可以用于数据拼接
select post ,group_concat(name) from emp group by post;
select post,group_concat(salary)from emp group by post;
数据拼接:
select post,group_concat(name,':',salary)from emp group by post
select post ,group_concat(name,'__nb')from emp group by post
hving过滤
having 和where的功能是一样的
where是用于分组之前的筛选
having是用于分组之后的筛选 
为了便于区分 将having称为过滤
统计各部门年龄在30岁以上的员工平均工资,并且保留平均工资大于10000的部门
select post avg(salary) from emp where age>=30 group by post having avg(salary)>10000;

image

distinct去重
# 去重的前提是数据必须一模一样
select distinct age from emp;

image

order by排序
select * from emp order by 字段名 asc; 默认升序
select * from emp order by 字段名 desc;降序
统计各部门年龄在10岁以上的员工平均工资,并且保留平均工资大于1000的部门,然后对平均工资进行排序
select post,avg(salary) from emp where age>10 group by post having avg(salary)>10000 order by avg(salary);

image

limit分页
 限制展示条数
 select * from emp limit 1;
 查询工资最高的人的详细信息
 select * from emp order by salary desc limit 1;
 
 分页显示
 select * from emp limit 0,5  # 第一个参数是起始位置,第二个参数是展示的数量
 select * from emp limit 5,5

image

regexp正则
# 在sql语句中写正则是为了更精准的筛选数据 
select * from emp where regexp '^j.*(n|y)$';

image

多表查询

子查询:将一张表的查询结果括号括起来作为另一个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 dep where id=200; #技术

用子查询
select name from dep where id=(select dep_id from emp1 where name='jason');

image

连表操作
select * from emp1,dep1;  # 笛卡尔积
# 这种连表写法不推荐 占用内存 效率低
连表操作的四个语法
1:inner join  内连接 只拼接两边都有的字段数据
2:left join   左连接 以左边为基准 展示所有的数据 没有的则用null填充
3:right j0in  右连接 以右边为基准 展示所有的数据 没有的则用null填充
4:union 全连接 不管有没有字段数据 有多少连接多少
select * from emp1 left join dep1 on emp1.dep_id=dep1.id  #连接的方式是按照员工表的部门id等于部门表的id
union
select * from emp1 right join dep1 on emp1.dep_id=dep1.id

image
image

标签:salary,name,之表,关键字,emp,MySQL,post,id,select
来源: https://www.cnblogs.com/Hsummer/p/16596910.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有