数据库数据:

# (1) 创建数据表

create table employee(

id int primary key auto_increment,

emp_name char(12) not null,

sex enum('male','female') not null default 'male', #大部分是男的

age int(3) unsigned not null default 28,

hire_date date not null,

post char(15),

post_comment varchar(100),

salary float(15,2),

office int, #一个部门一个屋子

depart_id int

);

# (2) 插入数据

insert into employee(emp_name,sex,age,hire_date,post,salary,office,depart_id) values

('xiaoyang','male',18,'20170301','独立安保部',7300.33,401,1), #以下是教学部

('laozhang','male',78,'20150302','teacher',1000000.31,401,1),

('xiaowu','male',81,'20130305','teacher',8300,401,1),

('daniu','male',73,'20140701','teacher',3500,401,1),

('laowu','male',28,'20121101','teacher',2100,401,1),

('xiaowang','female',18,'20110211','teacher',9000,401,1),

('zzdu','male',18,'19000301','teacher',30000,401,1),

('xiaolong','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)

;

一.where 条件的使用

#1.对表当中的数据进行筛选

#2.对值判断用:

= > < >= <= != <>(不等于)

#3.判断一个的范围

between 小值 and 大值 [小值,大值] 在两者之间的范围

in(值1,值2,值3....)   在括号里这个范围之内

#4.like 模糊查询 like '%' 通配符 匹配任意长度任意字符

like '%a' 以a结尾的任意长度字符串

like 'a%' 以a开头的任意长度字符串

like '%a%' 匹配中间带个a的字符串

like '_a'  一共2个长度,以a结尾,字符无所谓

like 'a__' 一共3个长度,以a开头,后面字符无所谓

#5.多个条件的拼接

and 两个链接都成立

or  有一个条件成立

not 非

# (1) 单条件查询:

# 查询部门是sale的所有员工姓名:

select emp_name from employee where post = 'sale';

# (2)多条件查询

# 部门是teacher , 收入大于10000

select * from employee where post="teacher" and salary>10000;

# (3)关键字 between .. and ..

# 收入在1万和2万之间的姓名和收入

select emp_name,salary from employee where salary between 10000 and 20000;

# 收入不再1万和2万之间的

select emp_name,salary from employee where salary not between 10000 and 20000;

# (4) 关键字is null(判断某个字段是否为null 不能用等号 用is)

#查询post_comment 是空的

select emp_name,post_comment from employee where post_comment is null;

#查询post_comment 不是空的

select emp_name,post_comment from employee where post_comment is not null;

#查询post_comment 是空字符串

select emp_name,post_comment from employee where post_comment = "";

# 设置一个值是空;

update employee set post_comment= '' where id = 2;

# (5) 关键字in集合查询

# 查收入是3000 或者3500或者4000或者9000所有的员工和收入

select emp_name,salary from employee where salary=3000 or salary=3500 or salary=4000 or salary=9000;

# 优化:在这个范围里查找

select emp_name,salary from employee where salary in (3000,3500,4000,9000);

# 不再这个范围里 float 的精度判断?

select emp_name,salary from employee where salary not in (3000,3500,4000,9000,3000.99);

# (6) 关键字like的模糊查询

## (1) 通配符'%'     [表示任意字符串]

select * from employee where emp_name like 'eg%';

## (2) 通配符'_'           [表示一个任意字符]

select * from employee where emp_name like 'al__';

# (7) sql 函数concat(参数1,参数2,参数3) 把所有参数拼接在一起

# concat_ws("符号",参数1,参数,参数3.....)

# sql 中可以做四则运算(加减乘除)

# as 用来起别名

select emp_name,concat("姓名:",emp_name,"薪水:",salary) as my_s  from employee;

select emp_name,concat("姓名:",emp_name,"薪水:",salary*12) as my_s  from employee;

# 第一个参数是分隔符,后面依次写上要拼接的值;

select emp_name,concat_ws(" : ",emp_name,salary*12) as my_s  from employee;

二.group by 分组

有几个种类就有几个数据

'''group by 分类 by后面的字段一般写在select 后面不会错'''

# select post from employee where depart_id > 1 group by post;

select office from employee where depart_id > 1 group by office;

# 函数: group_concat() 对分组的内容进行拼接;

select group_concat(emp_name),post from employee  where depart_id >1 group by post

# 聚合函数:

# count(*) : 统计总数

select count(*) from employee;

select count(*) from employee where depart_id = 1;

# 统计最大值 max

select max(salary) from employee;

# 统计最小值 min

select min(salary) from employee;

# 统计平均值 avg

select avg(salary) from employee;

# 统计总和 sum

select sum(salary)  from employee;

# 分组 + 聚合函数 一起使用

# 求各部门的平均工资

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

# 求各部门薪资的最大值

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

三.having

查完数据后再过滤:配合group by使用,主要用于分组之后的在过滤

'''执行过程:先执行where 在执行group by 等数据搜出来了再用having过滤的;'''

# 比如:求部门的平均薪资,在10000以上的所有部门

select post from employee  group by post having avg(salary) > 10000;

# 1查询各岗位内包含的员工个数小于2的    岗位名、岗位内包含员工名字、个数

select post,group_concat(emp_name),count(*) from employee group by post having count(*) <2;

# 2.查询各岗位平均薪资大于10000的岗位名、平均工资

select post,avg(salary) from employee group by post having avg(salary) > 10000;

#3查询各岗位平均薪资大于10000且小于20000的岗位名、平均工资

#(1)

select post,avg(salary) from employee group by post having avg(salary) > 10000 and avg(salary) < 20000;

#(2)

select post,avg(salary) from employee group by post having avg(salary)  between 10000 and 20000;

"""

select * from biao where 条件 group by 字段 having 条件表达式 order by 字段 limit

"""

四.order by

按照什么字段排序

# 默认升序asc 从小到大排序

select emp_name,age from employee where post = "teacher" order by age;

# 降序 desc   从大到小排序

select emp_name,age from employee where post = "teacher" order by age desc;

五.limit

限制查询的次数;[用来做数据分页的]

# 查询最后一条数据

select * from employee order by id desc limit 1;

select * from employee order by id desc limit 3;

# limit(m,n) 默认m的值是0 代表第一条数据,n所代表的是查询几条,从的m+1条数据开始,查询n条数据;

select * from employee limit 0,5

select * from employee limit 5,5

select * from employee limit 10,5

六.使用正则表达式查询数据

(了解,基本不用,效率非常低,与python正则部分结果不一样)

select * from employee where emp_name regexp '^ale.*';

select * from employee where  emp_name regexp 'on$';

select * from employee where salary regexp '0{3}';

# 用like来取代

select * from employee where emp_name like 'da%';

七.练习

# 练习:where

# 1. 查看岗位是teacher的员工姓名、年龄

select emp_name,age from employee where post = 'teacher';

# 2. 查看岗位是teacher且年龄大于30岁的员工姓名、年龄

select emp_name,age from employee where post = 'teacher' and age > 30;

# 3. 查看岗位是teacher且薪资在9000-1000范围内的员工姓名、年龄、薪资

# 法一:

select emp_name,age,salary from employee where post = 'teacher' and salary in (9000,10000);

#法二:

select emp_name,age,salary from employee where post = 'teacher' and salary between 9000 and 10000;

# 4. 查看岗位描述不为NULL的员工信息

select * from employee where post_comment is not null;

# 5. 查看岗位是teacher且薪资是10000或9000或30000的员工姓名、年龄、薪资

select emp_name,age,salary from employee where post = 'teacher' and salary in (10000,9000,3000);

# 6. 查看岗位是teacher且薪资不是10000或9000或30000的员工姓名、年龄、薪资

select emp_name,age,salary from employee where post = 'teacher' and salary not in (10000,9000,3000);

# 7. 查看岗位是teacher且名字是jin开头的员工姓名、年薪

select emp_name,salary*12 from employee where emp_name like 'zz%';

#练习:group

# 1. 查询岗位名以及岗位包含的所有员工名字

select post,group_concat(emp_name) from employee group by post;

# 2. 查询岗位名以及各岗位内包含的员工个数

select post,count(*) from employee group by post;

# 3. 查询公司内男员工和女员工的个数

select sex,count(*) from employee group by sex;

# 4. 查询岗位名以及各岗位的平均薪资

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

# 5. 查询岗位名以及各岗位的最高薪资

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

# 6. 查询岗位名以及各岗位的最低薪资

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

# 7. 查询男员工与男员工的平均薪资,女员工与女员工的平均薪资

select sex,avg(salary) from employee group by sex;

#练习:having

#1.查询各岗位内包含的员工个数小于2的岗位名、岗位内包含员工名字、个数

select post,group_concat(emp_name),count(*) from employee group by post having count(*) < 2;

#2.查询各岗位平均薪资大于10000的岗位名、平均工资

select post,avg(salary) from employee group by post having avg(salary) > 10000;

#3查询各岗位平均薪资大于10000且小于20000的岗位名、平均工资

select post,avg(salary) from employee group by post having avg(salary) > 10000 and avg(salary) < 20000;

#练习:order by

# 1. 查询所有员工信息,先按照age升序排序,如果age相同则按照hire_date降序排序

select * from employee  order by age,hire_date desc;

# 2. 查询各岗位平均薪资大于10000的岗位名、平均工资,结果按平均薪资升序排列

select post,avg(salary) from employee group by post having avg(salary) > 10000 order by avg(salary);

# 3. 查询各岗位平均薪资大于10000的岗位名、平均工资,结果按平均薪资降序排列

select post,avg(salary) from employee group by post having avg(salary) > 10000 order by avg(salary) desc;

MySQL 之单表查询的更多相关文章

  1. MySQL之单表查询 一 单表查询的语法 二 关键字的执行优先级(重点) 三 简单查询 四 WHERE约束 五 分组查询:GROUP BY 六 HAVING过滤 七 查询排序:ORDER BY 八 限制查询的记录数:LIMIT 九 使用正则表达式查询

    MySQL之单表查询 阅读目录 一 单表查询的语法 二 关键字的执行优先级(重点) 三 简单查询 四 WHERE约束 五 分组查询:GROUP BY 六 HAVING过滤 七 查询排序:ORDER B ...

  2. Mariadb/MySQL数据库单表查询基本操作及DML语句

    Mariadb/MySQL数据库单表查询基本操作及DML语句 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一数据库及表相关概述 1>.数据库操作 创建数据库: CREATE ...

  3. day 38 MySQL之单表查询

    MySQL之单表查询   阅读目录 一 单表查询的语法 二 关键字的执行优先级(重点) 三 简单查询 四 WHERE约束 五 分组查询:GROUP BY 六 HAVING过滤 七 查询排序:ORDER ...

  4. Mysql数据库单表查询

    1.单表查询语法 #查询数据的本质:mysql会到你本地的硬盘上找到对应的文件,然后打开文件,按照你的查询条件来找出你需要的数据.下面是完整的一个单表查询的语法 select * from,这个sel ...

  5. MySQL之单表查询、多表查询

    一.单表查询: 单个表的查询方法及语法顺序需要通过实际例子来熟悉 先将表数据创建下: mysql> create database singe_t1; # 建个数据库singe_t1 Query ...

  6. MySQL 的单表查询

    单表查询 语法: 一.单表查询的语法 SELECT 字段1,字段2 ,...FROM 表名 WHERE 条件 GROUP BY field HAVING 筛选 ORDER BY filed LIMIT ...

  7. 四 mysql之单表查询

    目录 一 单表查询的语法 二 关键字的执行优先级(重点) 三 简单查询 四 WHERE约束 五 分组查询:GROUP BY 1. 什么是分组?为什么要分组? 2. ONLY_FULL_GROUP_BY ...

  8. mysql(单表查询,多表查询,MySQl创建用户和授权,可视化工具Navicat的使用)

    单表查询 语法: 一.单表查询的语法 SELECT 字段1,字段2... FROM 表名 WHERE 条件 GROUP BY field HAVING 筛选 ORDER BY field LIMIT ...

  9. MySQL的单表查询

    单表查询 单表查询语法: select distinct 字段1,字段2... from 表名 where 条件 group by field having筛选 order by 关键字执行的优先级: ...

  10. day037 mysql之单表查询

    一.单表查询语法 select distinct 字段1,字段2,... from 库名.表名 where 条件 group by 字段 having 筛选条件 order by 字段 limit 限 ...

随机推荐

  1. Kubernetes 升级记录:从 1.16.3 升级至 1.17.0

    参考官方文档 Upgrading kubeadm clusters 在 ubuntu 18.04 上完成了升级,记录一下升级步骤. 一.升级第一个 master 节点 apt-get 安装 kubea ...

  2. Bugku-CTF分析篇-flag被盗(flag被盗,赶紧溯源!)

    flag被盗 flag被盗,赶紧溯源!

  3. python map、join函数

    map() 会根据提供的函数对指定序列做映射. 第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函数返回值的新列表. map(fun ...

  4. MySQL 学习(二)总体框架 & redo-log 和 bin-log 的介绍

    文章部分总结来自课程,非原创 MySQL 组织架构     下面这张图就可以解释关于 MySQL 底层的组织架构了.     上面的图可以直观地展示两个重要的东西 : 一条 SQL 的执行流程 MyS ...

  5. 银行T0理财怎么选

    ## 从现金管理说开去 现金是资产配置中的基石,买股票需要花掉现金,吃饭消费也要花掉现金.现金和我们的生活息息相关,需要慎重地管理起来,因此现金管理应运而生. 现金管理需要兼顾 *流动性* 和 *收益 ...

  6. 多租户SaaS的数据库设计模式

    前言 在设计多租户SaaS应用程序时,您必须仔细选择最适合您应用程序需求的租户模型.租户模型确定每个租户的数据如何映射到存储.您选择的租户模式会影响应用程序设计和管理.以后切换到另一个模型有时代价昂贵 ...

  7. selenium+python自动化用例登陆界面模板

    一.基本逻辑 1.自动填写用户名和密码登录成功后跳转到相应页面 2.验证相应页面的url与给定的url是否一致,如果一致则测试通过,如果不一致则不通过 二.以jenkins登陆界面为例,代码如下 fr ...

  8. java中Integer面试的坑

    class Test{ public static void main(String[] args){ //当值在[-128,127]中,不创建新的Integer Integer f1 = 100,f ...

  9. Vue - 让水平滚动条(scroll bar)固定在浏览器的底部

    效果 踩坑经历 TLDR; 在几个小时的google和stack overflow的苦苦搜索后,无果. 经过自我思考,想到了一种实现方法: 整个页面是一个盒子,要出现滚动条,必然里面的元素要溢出.也即 ...

  10. 关于 appium get_attribute 方法的坑

    (得要学着看源码) 问题: self.driver.find_element_by_id("id").get_attribute("content-desc") ...