首页 > 数据库 >MySQL之查询关键字

MySQL之查询关键字

时间:2022-08-17 21:27:39浏览次数:50  
标签:salary name 查询 关键字 emp MySQL post id select

今日内容

操作表的SQL语句补充

1.修改表名
	alter table 表名 remane 新表名;
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,
      gender 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,gender,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 name from emp;
		肯定是先支持from确定表 之后执行select确定字段
编写SQL语句针对select和from可以先写个固定模板
	select * from 表名 其他操作
select后的字段可能是实际的 也可能是通过SQL动态产生的 所以可以先用*占位最后再修改
'''
1.select
	自定义查询表中字段对应的说话
2.from
	指定操作的对象(到底来自哪张表 有可能是多张表)

查询关键字之where筛选

刚开始接触mysql查询的时候 建议按照查询的优先级顺序拼写出你的sql语句

1.先是查哪张表 from emp
2.再是根据什么条件去查 where id = 6
3.再是对查询出来的数据筛选展示部分 select name,salary

1.查询id大于等于3小于等于6的数据(两种方式)

select id,name from emp where id >= 3 and id <= 6;

select id,name from emp where id between 3 and 6;

image

2.查询薪资是20000或者18000或者17000的数据(两种方式)

select * from emp where salary = 20000 or salary = 18000 or salary = 17000;

select * from emp where salary in (20000,18000,17000);

image

3.查询员工姓名中包含o字母的员工姓名和薪资

select name,salary from emp where name like '%o%';

image

4.查询员工姓名是由四个字符组成的员工姓名与其薪资(两种方式)

select name,salary from emp where name like '____';

select name,salary from emp where char_length(name) = 4;

image

5.查询id小于3或者大于6的数据

select * from emp where id not between 3 and 6;

image

6.查询薪资不在20000,18000,17000范围的数据

select * from emp where salary not in (20000,18000,17000);

image

7.查询岗位描述为空的员工名与岗位名 针对null不能用等号,只能用is

select name,post from emp where post_comment = NULL;  # 查询为空

select name,post from emp where post_comment is NULL;

select name,post from emp where post_comment is not NULL;

image

查询关键字之group by分组

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

分组之后我们研究的对象应该是以组为单位 不应该再直接获取单个数据项 如果获取了应该直接报错 select后面可以直接填写的字段名只能是分组的依据(其他字段需要借助于一些方法才可以获取)
  set global sql_mode='strict_trans_tables,only_full_group_by';
我们写SQL是否需要使用分组 可以在题目中得到答案
  每个、平均、最大、最小

配置分组常见使用的有聚合函数

序号 关键字 作用
1. max 最大值
2. min 最小值
3. sum 总和
4. counnt 计数
5. avg 平均值

1.每个部门的最高薪资

select post,max(salary) from emp group by post;

image

2.每个部门的最低薪资

select post,min(salary) from emp group by post;

image

3.每个部门的平均薪资

select post,avg(salary) from emp group by post;

image

4.每个部门的工资总和

select post,sum(salary) from emp group by post;

image

5.每个部门的人数

select post,count(salary) from emp group by post;

image
6.在显示的时候还可以给字段取别名
as也可以省略 但是不推荐省 因为寓意不明确

mysql> select post as '部门',max(salary) as '最高薪资' from emp group by post;

mysql> select post '部门',max(salary) '最高薪资' from emp group by post;

image

group_conca(分组之后用)不仅可以用来显示除分组外字段还有拼接字符串的作用
1.每个人的名字

mysql> select post,group_concat(name) from emp group by post;

image

2.添加后缀

mysql> select post,group_concat(name,'_NB') from emp group by post;

image

3.拼接字符串

select post,group_concat(name,'薪资为: ',salary) from emp group by post;

image
4.每个人的薪资

mysql> select post,group_concat(salary) from emp group by post;

image

查询关键字之having过滤

where与having的功能其实是一样的 都是用来筛选数据
只不过where用于分组之前的筛选 而having用于分组之后的筛选
为了人为的区分 所以叫where是筛选 having是过滤

1.统计各部门年龄在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排序

1.升序(默认的)
select * from emp order by salary asc;

image

2.降序
select * from emp order by salary desc;

image

案例:统计各部门年龄在10岁以上的员工平均工资,并且保留平均工资大于1000的部门,然后对平均工资进行排序

select post,avg(salary) from emp where age > 10 group by post having avg(salary) > 1000 order by avg(salary);

image

查询关键字之limit分页

1.限制展示条数

select * from emp limit 3;

image

2.分页展示

第一个参数表示起始位置,第二个参数表示的是条数,不是索引位置

select * from emp limit 5,5;

image

查询关键字之regexp正则

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

image

多表查询思路

1.子查询
	将一张表查询结果用括号括起来当做另外一条SQL语句的条件
	eg:
		第一步干什么
		第二步基于第一步的结果在做操作
2.连表查询
	现将所有涉及到结果的表全部拼接到一起形成一张大表 然后从大表中查询数据
    
# 建表
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,
    dep1_id int
);

# 插入数据
insert into dep1 values
(200,'技术'),
(201,'人力资源'),
(202,'销售'),
(203,'运营'),
(205,'安保')
;

insert into emp1(name,gender,age,dep1_id) values
('jason','male',18,200),
('lisa','female',48,201),
('kevin','male',18,201),
('oscar','male',28,202),
('tony','male',18,203),
('jerry','female',18,204);

子查询

查询jason的部门名称

1.先获取jason的部门编号
	select dep1_id from emp1 where name = 'jason';  # 200
2.根据部门编号获取部门名称
	select name from dep1 where id = 200;
3.子查询
	select name from dep1 where id = (select dep1_id from emp1 where name = 'jason');

image

连表操作

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

image

1.inner join内连接

作用:只能拼接两边都有的字段
	select * from emp1 inner join dep1 on emp1.dep1_id = dep1.id;

image

2.left join左连接

作用:以左表为基准 展示所有的数据 没有对应数据则用NULL填充
	select * from emp1 left join dep1 on emp1.dep1_id = dep1.id;

image

3.right join 右连接

作用:以右变为基准 展示所有的数据 没有对应数据则用NULL填充
	select * from emp1 right join dep1 on emp1.dep1_id = dep1.id;

image

4.union 全连接

作用:左连接加右连接 左右两边的数据都有 没有数据的部分用NULL填充
	select * from emp1 left join dep1 on emp1.dep1_id = dep1.id 
	union 
	select *from emp1 right join dep1 on emp1.dep1_id = dep1.id;

image

标签:salary,name,查询,关键字,emp,MySQL,post,id,select
From: https://www.cnblogs.com/lisony/p/16596758.html

相关文章

  • Mysql使用实际01---SQL分组查询
    1.SQL分组查询使用场景对记录的分组是通过关键字GROUPBY实现的,GROUPBY后面跟着一个定义组的构成的属性列表。 如果我们使用语句GROUPBYA1,……,Ak我们就把记......
  • Django连接MySQL与正反向迁移命令
    目录连接MySQL方法一:pymysql连接方法二:mysqlclient迁移命令连接MySQL方法一:pymysql连接第一步:修改settings.py配置文件中的DATABASES:DATABASES={'default':{......
  • 【MySQL】多表查询:自连接VS非自连接
    区别:多表查询时是否和自身连接(自恋)1.自连接查询员工last_name和他的领导名称SELECTCONCAT(worker.last_name,'的领导是',manager.last_name)FROMemployeesworke......
  • static关键字
    java中的static是跟类一起加载的有static关键字的我们称之为静态变量,或者是静态方法例如:publicclassStudent{privatestaticintage;//静态变量多线程priv......
  • 【MySQL】多表查询:等值连接VS非等值连接
    区别:多表查询中的where条件是否相等1.等值连接查询员工的employee_id,last_name,department_name,citySELECTe.last_name,d.department_name,l.cityFROMemployeese,......
  • 【2022-08-17】mysql基础知识(四)
    mysql基础知识(四)mysql之操作表的多条SQL语句修改表名普通方法:altertabletest1renametest;进阶方法:renametabletesttotest1;可同时修改多个:renam......
  • Mysql 系列 | procedure(存储过程)
    存储过程是实现特定功能的语句集合,编译后存在数据库中。通过存储过程的名字,并给定参数来调用。创建createprocedure过程名([IN|OUT|INOUT参数名数据类型])begin......
  • 表操作与数据查询
    关于表的操作[]添加字段altertablet1addgendervarchar(32)deault"male";指定在某个字段后添加altertablet1addhobbyvarchar(32)aftername;在开头......
  • Winform下拉框使用拼音首字母查询
    项目需要一个搜索所在单位的选项,因为选项众多需要用下拉框来进行选择。众多选择为了更好的使用,采用了拼音首字母进行查询。显示的效果如果下:在输入框输入X出现X相关的单......
  • MySQL学习笔记
    MySQL学习笔记    MySQL语句中,字符串应当用单引号表示,而不是双引号,例如’Tom’;表中字段区分大小写,name和Name是不同的字段;命令不区分大小写,DROP和drop是相同的......