手撸Mysql原生语句--单表
select from where group by having order by limit
上面的所有操作是有执行的优先级的顺序的,我们将执行的过程可以总结为下面所示的七个步骤。
1.找到表:from
2.拿着where指定的约束条件,去文件/表中取出一条条记录
3.将取出的一条条记录进行分组group by,如果没有group by,则整体作为一组
4.执行select(去重)
5.将分组的结果进行having过滤
6.将结果按条件排序:order by
7.限制结果的显示条数
select from的使用
简单查询
SELECT id,emp_name,sex,age,hire_date,post,post_comment,salary,office,depart_id FROM employee;
SELECT * FROM employee;
SELECT emp_name,salary FROM employee;
避免重复DISTINCT
SELECT DISTINCT post FROM employee;
通过四则运算:
SELECT emp_name, salary*12 FROM employee;
SELECT emp_name, salary*12 AS Annual_salary FROM employee;
SELECT emp_name, salary*12 Annual_salary FROM employee;
1.查出所有员工的名字,薪资,格式为:<名字:egon> <薪资:3000>
select concat('<名字:',emp_name,'> ','<薪资:',salary,'>') from employee;
2.查询所有的岗位(去掉重复的岗位)
select distinct depart_id from employee;
3.查询所有员工的名字以及他们的年薪,年薪的字段名为annual_year
select emp_name, salary*12 annual_year from employee;
where约束
1.查看岗位是teacher的员工的姓名、年龄
select emp_name,age from emp_name where post = 'teacher';
2.查看岗位是teacher且年龄大于30岁的员工的姓名、年龄
select emp_name,age from employee where post ='teacher' and age >30;
3.查看岗位是teacher且薪资在9000-10000范围内的员工姓名、年龄、薪资
select emp_name,age,salary from employee where post ='teacher' and salary between 9000 and 10000;
select emp_name,age,salary from employee where post ='teacher' and salary >=9000 and salary <=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,30000);
6.查看岗位是teacher且薪资不是10000或9000或30000的员工姓名、年龄、薪资。
select emp_name,age,salary from employee where post='teacher' and salary not in (10000,9000,30000);
7.查看岗位是teacher且名字是jin开头的员工姓名、年薪
select emp_name,salary*12 annual_year from employee where post = 'teacher' and emp_name like 'jin%';
select emp_name,salary*12 annual_year from employee where post = 'teacher' and emp_name regexp '^jin';
group by--聚合(sum,avg,min,max,count)的使用
1.查询岗位名及岗位包含的所有员工名字
select post, group_concat(emp_name) from employee group by post;
select post, group_concat(emp_name) as emp_members from employee group by post;
2.查询岗位名以及岗位内包含的员工个数
select post,count(id) from employee group by post;
3.查询公司内男员工和女员工的个数
select sex,count(id) 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;
8.查询岗位名以及岗位包含员工的所有薪资的总和
select post,sum(salary) from employee group by post;
having过滤的使用
!!!执行优先级从高到低:where > group by > having
- Where 发生在分组group by之前,因而Where中可以有任意字段,但是绝对不能使用聚合函数。
- Having发生在分组group by之后,因而Having中可以使用分组的字段,无法直接取到其他字段,可以使用聚合函数
1.查询各岗位内包含的员工个数小于2的岗位名、岗位内包含员工名字、个数
select post,group_concat(emp_name),count(id) from employee group by post having count(id)<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) between 10000 and 20000;
select post,avg(salary) from employee group by post having avg(salary) > 10000 and avg(salary) <20000;
order by查询排序的使用
查询所有员工信息,先按照age升序排序,如果age相同则按照hire_date降序排序
select * from employee order by age asc,hire_date desc;查询各岗位平均薪资大于10000的岗位名、平均工资,结果按平均薪资升序排列
select post,avg(salary) from employee group by post having avg(salary)>10000 order by avg(salary) asc;查询各岗位平均薪资大于10000的岗位名、平均工资,结果按平均薪资降序排列
select post,avg(salary) from employee group by post having avg(salary)>10000 order by avg(salary) desc;
limit限制查询的记录数
select * from employee limit 0,5;
select * from employee limit 5,5;
select * from employee limit 10,5;
备注:limit后面的两个数字分别表示的含义为:第一个数字表示的含义是从第几条数据开始,第二个数字表示的是需要取出几条的数据。
使用正则表达式查询
select * from employee where emp_name REGEXP '^ale';
SELECT * FROM employee WHERE emp_name REGEXP 'on$';
SELECT * FROM employee WHERE emp_name REGEXP 'm{2}';
小结:对字符串匹配的方式
WHERE emp_name = 'egon';
WHERE emp_name LIKE 'yua%';
WHERE emp_name REGEXP 'on$';
1.查看所有员工中名字是jin开头,n或者g结果的员工信息。
select * from employee where emp_name regexp '^jin.*[ng]$';
手撸Mysql原生语句--单表的更多相关文章
- 手撸Mysql原生语句--多表
在开始之前,我们需要建立表,做建表和数据的准备的工作. 1.建表 create table department( id int, name varchar(20) ); create table e ...
- 手撸Mysql原生语句--增删改查
mysql数据库的增删改查有以下的几种的情况, 1.DDL语句 数据库定义语言: 数据库.表.视图.索引.存储过程,例如CREATE DROP ALTER SHOW 2.DML语句 数据库操纵语言: ...
- mysql——查询语句——单表查询——(概念)
一.基本查询语句 select的基本语法格式如下: select 属性列表 from 表名和视图列表 [ where 条件表达式1 ] [ group by 属性名1 [ having 条件表达式2 ...
- mysql——查询语句——单表查询——(示例)
一.基本查询语句 select的基本语法格式如下: select 属性列表 from 表名和视图列表 [ where 条件表达式1 ] [ group by 属性名1 [ having 条件表达式2 ...
- mysql 基础入门 单表查询
单表查询 select 表头,表头 as 别名 ,表头(+-*/的运算) from table_a 1.条件查询 where + 条件 <> , != 不等于 = 等于,也可以表示字符串值 ...
- mysql 数据操作 单表查询 目录
mysql 数据操作 单表查询 mysql 数据操作 单表查询 简单查询 避免重复DISTINCT mysql 数据操作 单表查询 通过四则运算查询 mysql 数据操作 单表查询 concat()函 ...
- mysql 数据操作 单表查询 where 约束 目录
mysql 数据操作 单表查询 where约束 between and or mysql 数据操作 单表查询 where约束 is null in mysql 数据操作 单表查询 where约束 li ...
- mysql 数据操作 单表查询 group by 分组 目录
mysql 数据操作 单表查询 group by 介绍 mysql 数据操作 单表查询 group by 聚合函数 mysql 数据操作 单表查询 group by 聚合函数 没有group by情况 ...
- MySQL数据库之单表查询中关键字的执行顺序
目录 MySQL数据库之单表查询中关键字的执行顺序 1 语法顺序 2 执行顺序 3 关键字使用语法 MySQL数据库之单表查询中关键字的执行顺序 1 语法顺序 select distinct from ...
随机推荐
- Manico--自定义应用快速切换
快速切换应用的app,使用非常频繁,奈何还是没有钱! 这玩意儿虽然免费,但是时不时跳一个弹框让你购买,也是够烦的,然后我们正好利用逆向工具,对着玩意儿进行破解,让它不再弹框! 下载安装Hopper D ...
- Java枚举简述
Java 枚举(enum) Java 枚举是一个特殊的类,一般表示一组常量,比如一年的 4 个季节,一个年的 12 个月份,一个星期的 7 天,方向有东南西北等. Java 枚举类使用 enum 关键 ...
- ASP.NET Core3.1使用IdentityServer4中间件系列随笔(二):创建API项目,配置IdentityServer保护API资源
配套源码:https://gitee.com/jardeng/IdentitySolution 接上一篇<ASP.NET Core3.1使用IdentityServer4中间件系列随笔(一):搭 ...
- 23种设计模式 - 行为变化(Command - Visitor)
其他设计模式 23种设计模式(C++) 每一种都有对应理解的相关代码示例 → Git原码 ⌨ 行为变化 Command 动机(Motivation) 在软件构建过程中,"行为请求者" ...
- 现在的市场对 C++ 的需求大吗?
分享 大师助手 先说结论:需求还是很大,但是没有什么初级程序员能干的岗位. 游戏引擎,存储,推荐引擎,infra,各种各样的性能敏感场景.这些都是C++的刚需场景,别的语言基本替代不了的.除了pin ...
- 分享几个好用的ui框架,以便开发
1:Layui--经典模块化前端框架 地址:https://www.layui.com/ 2:iview--基于 Vue.js 的高质量 UI 组件库 地址:http://v1.iviewui.com ...
- 使用【QQ五笔的码表】转成【百度手机自定义码表】
使用[QQ五笔码表]转成[百度手机自定义码表] QQ五笔码表先转成多多格式. 这里选用极点>>多多. 然后去掉空格. 转成GB.去掉没的字. 百度要的是这种格式. 现再用点讯工具转成 de ...
- 入门的艰难——关于LR的使用
这年头做一件事真是TM不容易啊.做测试也很纠结,不是都说商业工具很强大么,我去,这个不支持那个不支持的,这还有什么搞头,还非要按照你说的这个版本的才行,高一点的就crash了,结果连最初级的录制脚本都 ...
- Lua索引、伪索引、引用
索引:堆栈的索引 伪索引:一个类似于索引,但是有着特殊存储的索引,使用方式和索引一样,看上去像在操作堆栈 引用:LUA_REGISTRYINDEX伪索引下的表的整数键
- [HGAME Week2] Cosmos的博客后台
觉得这道题考察的东西比较综合而且比较简单,就写上了.因为写这篇文章的时候环境已经关闭了,所以引用了其他师傅wp的图片 本题考察了:php://filter伪协议文件包含.var_dump()输出GLO ...