MySQL单表查询实例
数据表准备
```mysql
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','teacher',7300.33,401,1), #以下是教学部
('egon','male',78,'20150302','teacher',1000000.31,401,1),
('kevin','male',81,'20130305','teacher',8300,401,1),
('tank','male',73,'20140701','teacher',3500,401,1),
('owen','male',28,'20121101','teacher',2100,401,1),
('jerry','female',18,'20110211','teacher',9000,401,1),
('nick','male',18,'19000301','teacher',30000,401,1),
('sean','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)
;
查询语句的执行顺序
先看一个简单的语句
select id,name from emp where id >= 3 and id <= 6;
这个表的查询语句的执行顺序是什么呢?
MySQL 从 from 后面的表名中去寻找当前数据库中有没有这个表,如果存在 去 where 后面的条件中定位一条或几条数据,将数据教给 select 去筛选其中的字段。
where约束条件
常见的查询
查询id大于等于3小于等于6的数据
select id,name from emp where id >= 3 and id <= 6;
select * from emp where id between 3 and 6;
查询id大于等于3小于等于6的数据
select * from emp where salary = 20000 or salary = 18000 or salary = 17000;
select * from emp where salary in (20000,18000,17000);
查询员工姓名中包含o字母的员工姓名和薪资(模糊查询)
select name,salary from emp where name like '%o%';
查询员工姓名是由四个字符组成的员工姓名与其薪资
select name,salary from emp where name like '____'; # _ 代表一个字符
select name,salary from emp where char_length(name) = 4;
查询id小于3或者大于6的数据
select * from emp where id not between 3 and 6;
查询薪资不在20000,18000,17000范围的数据
select * from emp where salary not in (20000,18000,17000);
查询岗位描述为空的员工名与岗位名 针对null不能用等号,只能用is
select name,post from emp where post_comment is NULL;
select name,post from emp where post_comment is not NULL;
GroupBy查询(分组查询)
按部门分组
select * from emp group by post; # 分组后取出的是每个组的第一条数据
设置sql_mode为only_full_group_by,意味着以后但凡分组,只能取到分组的依据,不应该在去取组里面的单个元素的值,那样的话分组就没有意义了,因为不分组就是对单个元素信息的随意获取
在mysql客户端输入下面命令,并重新连接
set global sql_mode="strict_trans_tables,only_full_group_by";
select id,name,sex from emp group by post; # 报错
select post from emp group by post; # 获取部门信息
获取每个部门的最高工资
以组为单位统计组内数据>>>聚合查询(聚集到一起合成为一个结果)
# 每个部门的最高工资
select post,max(salary) from emp group by post;
# 每个部门的最低工资
select post,min(salary) from emp group by post;
# 每个部门的平均工资
select post,avg(salary) from emp group by post;
# 每个部门的工资总和
select post,sum(salary) from emp group by post;
# 每个部门的人数
select post,count(id) from emp group by post;
查询分组之后的部门名称和每个部门下所有的学生姓名
group_concat(分组之后用)不仅可以用来显示除分组外字段还有拼接字符串的作用
select post,group_concat(name) from emp group by post;
mysql> select post,group_concat(name) from emp group by post;
+-----------+------------------------------------------------+
| post | group_concat(name) |
+-----------+------------------------------------------------+
| operation | 张野,程咬金,程咬银,程咬铜,程咬铁 |
| sale | 歪歪,丫丫,丁丁,星星,格格 |
| teacher | jason,egon,kevin,tank,owen,jerry,nick,sean |
+-----------+------------------------------------------------+
select post,group_concat(name,": ",salary) from emp group by post;
+-----------+-------------------------------------------------------------------------------------------------------------------------+
| post | group_concat(name,": ",salary)
|
+-----------+-------------------------------------------------------------------------------------------------------------------------+
| operation | 张野: 10000.13,程咬金: 20000.00,程咬银: 19000.00,程咬铜: 18000.00,程咬铁: 17000.00
|
| sale | 歪歪: 3000.13,丫丫: 2000.35,丁丁: 1000.37,星星: 3000.29,格格: 4000.33
|
| teacher | jason: 7300.33,egon: 1000000.31,kevin: 8300.00,tank: 3500.00,owen: 2100.00,jerry: 9000.00,nick: 30000.00,sean: 10000.00 |
+-----------+--------------------------------------------------------------------------------------------------------------
select post,group_concat(salary) from emp group by post;
mysql> select post,group_concat(salary) from emp group by post;
+-----------+----------------------------------------------------------------------+
| post | group_concat(salary) |
+-----------+----------------------------------------------------------------------+
| operation | 10000.13,20000.00,19000.00,18000.00,17000.00 |
| sale | 3000.13,2000.35,1000.37,3000.29,4000.33 |
| teacher | 7300.33,1000000.31,8300.00,3500.00,2100.00,9000.00,30000.00,10000.00 |
+-----------+----------------------------------------------------------------------+
3 rows in set (0.00 sec)
⚠️ 注意
关键字where group by同时出现的情况下,group by必须在where之后 , where先对整张表进行一次筛选,如何group by再对筛选过后的表进行分组
having
having的语法格式与where一致,只不过having是在分组之后进行的过滤,即where虽然不能用聚合函数,但是having可以!
统计各部门年龄在30岁以上的员工平均工资,并且保留平均工资大于10000的部门
select post,avg(salary) from emp
where age >= 30
group by post
having avg(salary) > 10000;
⚠️having必须在group by后面使用不然会报错
select * from emp having avg(salary) > 10000;
distinct
对有重复的展示数据进行去重操作
select distinct post from emp;
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
# 限制展示条数
select * from emp limit 3;
# 查询工资最高的人的详细信息
select * from emp order by salary desc limit 1;
# 分页显示
select * from emp limit 0,5; # 第一个参数表示起始位置,第二个参数表示的是条数,不是索引位置
select * from emp limit 5,5;
正则
MySQL对正则表达式的支持只有一部分,当你常用的正则表达语句匹配不出相应的结果的时候还请注意一下。
select * from emp where name regexp '^j.*(n|y)$';
MySQL单表查询实例的更多相关文章
- Mysql 单表查询 子查询 关联查询
数据准备: ## 学院表create table department( d_id int primary key auto_increment, d_name varchar(20) not nul ...
- python 3 mysql 单表查询
python 3 mysql 单表查询 1.准备表 company.employee 员工id id int 姓名 emp_name varchar 性别 sex enum 年龄 age int 入职 ...
- Mysql 单表查询-排序-分页-group by初识
Mysql 单表查询-排序-分页-group by初识 对于select 来说, 分组聚合(((group by; aggregation), 排序 (order by** ), 分页查询 (limi ...
- Mysql 单表查询where初识
Mysql 单表查询where初识 准备数据 -- 创建测试库 -- drop database if exists student_db; create database student_db ch ...
- MySQL单表查询
MySQL之单表查询 创建表 # 创建表 mysql> create table company.employee5( id int primary key AUTO_INCREMENT not ...
- python mysql 单表查询 多表查询
一.外键 变种: 三种关系: 多对一 站在左表的角度: (1)一个员工 能不能在 多个部门? 不成立 (2)多个员工 能不能在 一个部门? 成立 只要有一个条件成立:多 对 一或者是1对多 如果两个条 ...
- mysql 单表查询
一 单表查询的语法 SELECT 字段1,字段2... FROM 表名 WHERE 条件 GROUP BY field HAVING 筛选 ORDER BY field LIMIT 限制条数 二 ...
- SQL学习笔记四(补充-1)之MySQL单表查询
阅读目录 一 单表查询的语法 二 关键字的执行优先级(重点) 三 简单查询 四 WHERE约束 五 分组查询:GROUP BY 六 HAVING过滤 七 查询排序:ORDER BY 八 限制查询的记录 ...
- python开发mysql:单表查询&多表查询
一 单表查询,以下是表内容 一 having 过滤 1.1 having和where select * from emp where id > 15; 解析过程;from > where ...
随机推荐
- pyinstaller发布exe,弹出Failed to execute script main
1.在PyCharm中按Alt+F12打开Terminal对话框 1.1我的项目文件放在wxpython目录下,D:\learn\Weather index insurance\wxpython> ...
- centos7操作
一.安装系统 1.下载(Minimal ISO)http://isoredirect.centos.org/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-1 ...
- docker 导出多个镜像合并成一个tar
导出单个镜像 docker save [images] > [name.tar] 倒出多个镜像合并成一个tar包 docker save [images] [images] > [name ...
- python-Web-django-商城-购物车商品加减
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Mybatis 属性配置
properties 定义配置,配置的属性可以在整个配置文件中的其他位置进行引用 <properties resource="db.properties"></ ...
- 爬取汽车之家新闻图片的python爬虫代码
import requestsfrom bs4 import BeautifulSouprespone=requests.get('https://www.autohome.com.cn/news/' ...
- mysql 插入数据后返回自增 ID 的七种方法
参考地址:https://blog.csdn.net/qq_30715329/article/details/80868411 其中使用函数方式.存储过程方式.注解方式.xml属性方式设置都可. 常用 ...
- [bzoj4026]dC Loves Number Theory_主席树_质因数分解_欧拉函数
dC Loves Number Theory 题目大意:dC 在秒了BZOJ 上所有的数论题后,感觉萌萌哒,想出了这么一道水题,来拯救日益枯竭的水题资源. 给定一个长度为 n的正整数序列A,有q次询问 ...
- [转帖]yum与apt-get的区别以及两者更新源(阿里/网易【163】)
yum与apt-get的区别以及两者更新源(阿里/网易[163]) 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/one_super_dreamer ...
- *【Python】【demo实验30】【练习实例】【使用Turtle实现实时时钟效果】
目的: 使用Turtle实现实时时钟效果 源代码: # encoding=utf-8 # -*- coding: UTF-8 -*- import turtle from datetime impor ...