【MySQL】笔记(2)— 部分 DQL 语句;条件查询;排序;分组函数;单行处理函数;group by ,having ;

1.简单的查询语句(DQL):

select 字段名1,字段名2,字段名3,…. from 表名;

注意:
1、任何一条sql语句都以”;”结尾;
2、sql语句不区分大小写;

查询员工的年薪情况?(场可以参与数学运算)

[En]

Inquire about the annual salary of employees? (fields can participate in mathematical operations)

select ename,sal * 12 from emp;
+——–+———-+
| ename | sal * 12 |
+——–+———-+
| SMITH | 9600.00 |
| ALLEN | 19200.00 |
| WARD | 15000.00 |
| JONES | 35700.00 |
| MARTIN | 15000.00 |
| BLAKE | 34200.00 |
| CLARK | 29400.00 |
| SCOTT | 36000.00 |
| KING | 60000.00 |
| TURNER | 18000.00 |
| ADAMS | 13200.00 |
| JAMES | 11400.00 |
| FORD | 36000.00 |
| MILLER | 15600.00 |
+——–+———-+

给查询结果的列重命名?
select ename,sal * 12 as yearsal from emp;

别名中有中文?
select ename,sal * 12 as 年薪 from emp; // 错误
select ename,sal * 12 as ‘年薪’ from emp; // 正确
+——–+———-+
| ename | 年薪 |
+——–+———-+
| SMITH | 9600.00 |
| ALLEN | 19200.00 |
| WARD | 15000.00 |
| JONES | 35700.00 |
| MARTIN | 15000.00 |
| BLAKE | 34200.00 |
| CLARK | 29400.00 |
| SCOTT | 36000.00 |
| KING | 60000.00 |
| TURNER | 18000.00 |
| ADAMS | 13200.00 |
| JAMES | 11400.00 |
| FORD | 36000.00 |
| MILLER | 15600.00 |
+——–+———-+

注意:标准sql语句中要求字符串使用单引号括起来,虽然mysql支持双引号,尽量别用;

as 关键字可以省略;

查询所有字段?
select * from emp; // 实际开发中不建议使用*,效率较低

2.条件查询:

查询工资等于5000的员工姓名?
select ename from emp where sal = 5000;
+——-+
| ename |
+——-+
| KING |
+——-+
查询SMITH的工资?
select sal from emp where ename = ‘SMITH’; // 字符串使用单引号括起来。
+——–+
| sal |
+——–+
| 800.00 |
+——–+
找出工资高于3000的员工?
select ename,sal from emp where sal > 3000;

类比:

select ename,sal from emp where sal >= 3000;

select ename,sal from emp where sal < 3000;

select ename,sal from emp where sal

select ename,sal from emp where sal <> 3000;

select ename,sal from emp where sal != 3000;

找出工资在1100和3000之间的员工(包括1100和3000)?
select ename,sal from emp where sal >= 1100 and sal

select ename,sal from emp where sal between 1100 and 3000; // between…and…是闭区间 [1100 ~ 3000]

select ename,sal from emp where sal between 3000 and 1100;

// 查询不到任何数据(between and在使用的时候必须左小右大)

between and除了可以使用在数字方面之外,还可以使用在字符串方面:
select ename from emp where ename between ‘A’ and ‘C’; // 左闭右开
+——-+
| ename |
+——-+
| ALLEN |
| BLAKE |
| ADAMS |
+——-

找出哪些人津贴为NULL?
数据库当中NULL不是一个值,代表什么也没有,为空。
空不是一个值,不能用等号衡量;必须使用 is null或者is not null
select ename,sal,comm from emp where comm is null;
+——–+———+——+
| ename | sal | comm |
+——–+———+——+
| SMITH | 800.00 | NULL |
| JONES | 2975.00 | NULL |
| BLAKE | 2850.00 | NULL |
| CLARK | 2450.00 | NULL |
| SCOTT | 3000.00 | NULL |
| KING | 5000.00 | NULL |
| ADAMS | 1100.00 | NULL |
| JAMES | 950.00 | NULL |
| FORD | 3000.00 | NULL |
| MILLER | 1300.00 | NULL |
+——–+———+——+
select ename,sal,comm from emp where comm = null;
Empty set (0.00 sec)

找出哪些人津贴不为NULL?
select ename,sal,comm from emp where comm is not null;
+——–+———+———+
| ename | sal | comm |
+——–+———+———+
| ALLEN | 1600.00 | 300.00 |
| WARD | 1250.00 | 500.00 |
| MARTIN | 1250.00 | 1400.00 |
| TURNER | 1500.00 | 0.00 |
+——–+———+———+

找出哪些人没有津贴?
select ename,sal,comm from emp where comm is null or comm = 0;
+——–+———+——+
| ename | sal | comm |
+——–+———+——+
| SMITH | 800.00 | NULL |
| JONES | 2975.00 | NULL |
| BLAKE | 2850.00 | NULL |
| CLARK | 2450.00 | NULL |
| SCOTT | 3000.00 | NULL |
| KING | 5000.00 | NULL |
| TURNER | 1500.00 | 0.00 |
| ADAMS | 1100.00 | NULL |
| JAMES | 950.00 | NULL |
| FORD | 3000.00 | NULL |
| MILLER | 1300.00 | NULL |
+——–+———+——+

找出工作岗位是MANAGER和SALESMAN的员工?
select ename,job from emp where job = ‘MANAGER’ or job = ‘SALESMAN’;
+——–+———-+
| ename | job |
+——–+———-+
| ALLEN | SALESMAN |
| WARD | SALESMAN |
| JONES | MANAGER |
| MARTIN | SALESMAN |
| BLAKE | MANAGER |
| CLARK | MANAGER |
| TURNER | SALESMAN |
+——–+———-+

and和or联合起来用:找出薪资大于1000的并且部门编号是20或30部门的员工。
select ename,sal,deptno from emp where sal > 1000 and deptno = 20 or deptno = 30; // 错误
select ename,sal,deptno from emp where sal > 1000 and (deptno = 20 or deptno = 30);

// 正确(注意:当运算符的优先级不确定的时候加小括号)

in等同于or:找出工作岗位是MANAGER和SALESMAN的员工?
select ename,job from emp where job = ‘SALESMAN’ or job = ‘MANAGER’;
select ename,job from emp where job in(‘SALESMAN’, ‘MANAGER’);// 与上一行语句等价

select ename,job from emp where sal in(800, 5000); // in后面的值不是区间,是具体的值

not in: 不在这几个值当中:
select ename,job from emp where sal not in(800, 5000);

模糊查询like ? (针对字符串)
找出名字当中含有O的?(在模糊查询当中,必须掌握两个特殊的符号,一个是%,一个是_)
%代表任意多个字符,_代表任意1个字符。
select ename from emp where ename like ‘%O%’;
+——-+
| ename |
+——-+
| JONES |
| SCOTT |
| FORD |
+——-+
找出名字中第二个字母是A的?
select ename from emp where ename like ‘_A%’;
+——–+
| ename |
+——–+
| WARD |
| MARTIN |
| JAMES |
+——–+
找出名字中有下划线的?

select name from t_user where name like ‘%_%’;(错误)
+———-+
| name |
+———-+
| zhangsan |
| lisi |
| WANG_WU |
+———-+
select name from t_user where name like ‘%_%’;(正确)
+———+
| name |
+———+
| WANG_WU |
+———+

找出名字中最后一个字母是T的?
select ename from emp where ename like ‘%T’;
+——-+
| ename |
+——-+
| SCOTT |
+——-+

3. 排序(升序、降序)

按照工资的升序找出员工的姓名和工资?

[En]

Find out the employee’s name and salary according to the rising order of salary?

select
ename,sal
from
emp
order by
sal;
+——–+———+
| ename | sal |
+——–+———+
| SMITH | 800.00 |
| JAMES | 950.00 |
| ADAMS | 1100.00 |
| WARD | 1250.00 |
| MARTIN | 1250.00 |
| MILLER | 1300.00 |
| TURNER | 1500.00 |
| ALLEN | 1600.00 |
| CLARK | 2450.00 |
| BLAKE | 2850.00 |
| JONES | 2975.00 |
| FORD | 3000.00 |
| SCOTT | 3000.00 |
| KING | 5000.00 |
+——–+———+

注意:默认是升序。怎么指定升序或者降序呢?asc表示升序,desc表示降序
select ename , sal from emp order by sal;// 升序
select ename , sal from emp order by sal asc; // 升序
select ename , sal from emp order by sal desc; // 降序

按薪资降序排列,薪资相同时按名称升序排列。

[En]

It is arranged in descending order of salary, and in ascending order of name when the salary is the same.

select ename,sal from emp order by sal desc;
select ename,sal from emp order by sal desc , ename asc;
注:场越高,越占优势。仅当无法对前面的字段进行排序时,才会启用后几个字段。

[En]

Note: the higher the field is, the more dominant it is. The latter fields are enabled only if the previous fields cannot be sorted.

找出工作岗位是SALESMAN的员工,并且要求按照薪资的降序排列。
select
ename,job,sal
from
emp
where
job = ‘SALESMAN’
order by
sal desc;
+——–+———-+———+
| ename | job | sal |
+——–+———-+———+
| ALLEN | SALESMAN | 1600.00 |
| TURNER | SALESMAN | 1500.00 |
| WARD | SALESMAN | 1250.00 |
| MARTIN | SALESMAN | 1250.00 |
+——–+———-+———+

4. 分组函数:所有的分组函数都是对”某一组”数据进行操作的;

count 计数
sum 求和
avg 平均值
max 最大值
min 最小值

找出工资总和?
select sum(sal) from emp;
找出最高工资?
select max(sal) from emp;
找出最低工资?
select min(sal) from emp;
找出平均工资?
select avg(sal) from emp;
找出总人数?
select count(*) from emp;
select count(ename) from emp;

count()和count(具体的某个字段),他们有什么区别?
count(
):不是统计某个字段中数据的个数,而是统计总记录条数。(和某个字段无关)
count(comm): 表示统计comm字段中不为NULL的数据总数量。

分组函数还有一个名字:多行处理函数;多行处理函数的特点:输入为多行,最终输出结果为1行。

[En]

The grouping function has another name: the multiline processing function; the characteristic of the multiline processing function: the input is multiple lines, and the final output result is 1 line.

分组函数自动忽略NULL。
select count(comm) from emp;
+————-+
| count(comm) |
+————-+
| 4 |
+————-+

select sum(comm) from emp;
+———–+
| sum(comm) |
+———–+
| 2200.00 |
+———–+

找到工资高于平均工资的员工?

[En]

Find employees whose wages are higher than the average salary?

select ename,sal from emp where sal > avg(sal);//ERROR 1111 (HY000): Invalid use of group function
考虑一下上面的错误消息:无效使用分组函数?

[En]

Consider the above error message: invalid use of grouping functions?

原因:SQL语句当中有一个语法规则,分组函数不可直接使用在where子句当中。why????

怎么解释?
group by是在where执行之后才会执行的!

分组功能还可以组合在一起以:

[En]

Grouping functions can also be combined to:

select count(*),sum(sal),avg(sal),max(sal),min(sal) from emp;

+———-+———-+————-+———-+———-+
| count(*) | sum(sal) | avg(sal) | max(sal) | min(sal) |
+———-+———-+————-+———-+———-+
| 14 | 29025.00 | 2073.214286 | 5000.00 | 800.00 |
+———-+———-+————-+———-+———-+

找到工资高于平均工资的员工?

[En]

Find employees whose wages are higher than the average salary?

第一步:找出平均工资
select avg(sal) from emp;
+————-+
| avg(sal) |
+————-+
| 2073.214286 |
+————-+
第二步:寻找工资高于平均水平的员工

[En]

Step 2: find employees with higher-than-average wages

select ename,sal from emp where sal > 2073.214286;
+——-+———+
| ename | sal |
+——-+———+
| JONES | 2975.00 |
| BLAKE | 2850.00 |
| CLARK | 2450.00 |
| SCOTT | 3000.00 |
| KING | 5000.00 |
| FORD | 3000.00 |
+——-+———+

合体版:select ename,sal from emp where sal > (select avg(sal) from emp);

5.单行处理函数:输入一行,输出一行

计算每个员工的年薪?
select ename,(sal+comm)*12 as yearsal from emp;
重点:所有数据库都是这样规定的,只要有NULL参与的运算结果一定是NULL。
使用ifnull函数:
select ename,(sal+ifnull(comm,0))*12 as yearsal from emp;

ifnull() 空处理函数:
ifnull(可能为NULL的数据,被当做什么处理) : 属于单行处理函数。
select ename,ifnull(comm,0) as comm from emp;
+——–+———+
| ename | comm |
+——–+———+
| SMITH | 0.00 |
| ALLEN | 300.00 |
| WARD | 500.00 |
| JONES | 0.00 |
| MARTIN | 1400.00 |
| BLAKE | 0.00 |
| CLARK | 0.00 |
| SCOTT | 0.00 |
| KING | 0.00 |
| TURNER | 0.00 |
| ADAMS | 0.00 |
| JAMES | 0.00 |
| FORD | 0.00 |
| MILLER | 0.00 |
+——–+———+

6. group by 和 having

group by : 按照某个字段或者某些字段进行分组。
having : having是对分组之后的数据进行再次过滤。

案例研究:找出每一份工作的最高工资。

[En]

Case study: find out the maximum salary for each job.

select max(sal),job from emp group by job;

+———-+———–+
| max(sal) | job |
+———-+———–+
| 3000.00 | ANALYST |
| 1300.00 | CLERK |
| 2975.00 | MANAGER |
| 5000.00 | PRESIDENT |
| 1600.00 | SALESMAN |
+———-+———–+

注意:分组函数一般都会和group by联合使用,这也是为什么它被称为分组函数的原因,并且任何一个分组函数(count sum avg max min)都是在group by语句执行结束之后才会执行的,当一条sql语句没有group by的话,整张表的数据会自成一组。

select ename,max(sal),job from emp group by job;
以上在mysql当中,查询结果是有的,但是结果没有意义(在Oracle数据库当中会报错。语法错误);
记住一个规则:当一条语句中有group by的话,select后面只能跟分组函数和参与分组的字段。

每份工作的平均工资是多少?

[En]

What is the average salary per job?

select job,avg(sal) from emp group by job;
+———–+————-+
| job | avg(sal) |
+———–+————-+
| ANALYST | 3000.000000 |
| CLERK | 1037.500000 |
| MANAGER | 2758.333333 |
| PRESIDENT | 5000.000000 |
| SALESMAN | 1400.000000 |
+———–+————-+

找出每个部门的最高工资,并要求显示工资大于2900的数据。

[En]

Find out the maximum salary for each department and require that data with a salary greater than 2900 be displayed.

第一步:找出每个部门的最高工资

[En]

Step 1: find out the maximum salary for each department

select max(sal),deptno from emp group by deptno;
+———-+——–+
| max(sal) | deptno |
+———-+——–+
| 5000.00 | 10 |
| 3000.00 | 20 |
| 2850.00 | 30 |
+———-+——–+

第二步:找出薪资大于2900
select max(sal),deptno from emp group by deptno having max(sal) > 2900;// 这种方式效率低
+———-+——–+
| max(sal) | deptno |
+———-+——–+
| 5000.00 | 10 |
| 3000.00 | 20 |
+———-+——–+

合体版:select max(sal),deptno from emp where sal > 2900 group by deptno;

// 效率较高,建议能够使用where过滤的尽量使用where
+———-+——–+
| max(sal) | deptno |
+———-+——–+
| 5000.00 | 10 |
| 3000.00 | 20 |
+———-+——–+

找出每个部门的平均工资,并索要工资超过2000的数据。

[En]

Find out the average salary for each department and ask for data with a salary greater than 2000.

第一步:找出每个部门的平均工资

[En]

Step 1: find out the average salary of each department

select deptno,avg(sal) from emp group by deptno;
+——–+————-+
| deptno | avg(sal) |
+——–+————-+
| 10 | 2916.666667 |
| 20 | 2175.000000 |
| 30 | 1566.666667 |
+——–+————-+

第二步:需要显示薪资大于2000的数据

[En]

Step 2: require to display data with a salary greater than 2000

select deptno,avg(sal) from emp group by deptno having avg(sal) > 2000;
+——–+————-+
| deptno | avg(sal) |
+——–+————-+
| 10 | 2916.666667 |
| 20 | 2175.000000 |
+——–+————-+

where后面不能使用分组函数:
select deptno,avg(sal) from emp where avg(sal) > 2000 group by deptno; //错误,所以这种情况只能使用having过滤;

关于查询结果集的去重?
mysql> select distinct job from emp;// distinct关键字去除重复记录。
+———–+
| job |
+———–+
| CLERK |
| SALESMAN |
| MANAGER |
| ANALYST |
| PRESIDENT |
+———–+

mysql> select ename,distinct job from emp;

//以上的sql语句是错误的,记住:distinct只能出现在所有字段的最前面

mysql> select distinct deptno,job from emp;
+——–+———–+
| deptno | job |
+——–+———–+
| 20 | CLERK |
| 30 | SALESMAN |
| 20 | MANAGER |
| 30 | MANAGER |
| 10 | MANAGER |
| 20 | ANALYST |
| 10 | PRESIDENT |
| 30 | CLERK |
| 10 | CLERK |
+——–+———–+

案例:统计岗位的数量?
select count(distinct job) from emp;

+———————+
| count(distinct job) |
+———————+
| 5 |
+———————+

6. 目前SQL语句执行顺序:

select (5)
..

from (1)
..

where (2)
..

group by (3)
..

having (4)
..

order by (6)
.. ;

Original: https://www.cnblogs.com/Burning-youth/p/15676723.html
Author: 猿头猿脑的王狗蛋
Title: 【MySQL】笔记(2)— 部分 DQL 语句;条件查询;排序;分组函数;单行处理函数;group by ,having ;

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/507966/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • 博客园美化-随季节变化实现不同的飘落效果

    最近在研究博客园的美化效果,看到有一个樱花飘落的效果,忽然突发奇想,如果能根据当前日期所处的季节实现不同的飘落效果岂不是更酷。😂 最近在研究博客园的美化效果,看到有一个樱花飘落的效…

    数据库 2023年6月6日
    0107
  • MYSQL事务篇(高级篇)

    1.事务介绍: 一般是指要做的或所做的事情。 在计算机 术语 中是指访问并可能更新数据库中各种 数据项 的一个程序 执行单元 (unit) 2.数据库事务具有ACID四大特性 。 …

    数据库 2023年5月24日
    077
  • [Linux]如何将一个目录添加至环境变量

    全局环境变量有两个目录,其中 /etc/profile 仅初始化登陆 shell 的环境变量 /etc/bash.bashrc 仅初始化交互 shell 为了在各种终端中使用,这里…

    数据库 2023年6月16日
    086
  • ECMAScript版本知识点汇总

    ECMAScript版本知识点汇总 ES5 btoa、atob 对参数进行base64格式编码、解码 /** * btoa() * base64编码 * @param {strin…

    数据库 2023年6月11日
    0103
  • 当mysql表从压缩表变成普通表会发生什么

    本文章做了把mysql表从压缩表过渡到普通表的实验过程,看看压缩表变成普通表会发生什么?本文针对mysql5.7和mysql8分别进行了实验。 1、什么是表压缩 在介绍压缩表变成普…

    数据库 2023年6月16日
    075
  • mysql解压版简洁式本地配置方式

    1. 设置全局变量 解压mysql压缩包到指定位置, 然后配置全局变量, 在 path 中添加全局变量, 值为 mysql 根目录下 bin 目录路径, 比如: D:\code_s…

    数据库 2023年6月14日
    074
  • Django点击图片缩放

    参考信息 用 zoom.js 给博客园中博文的图片添加单击时弹出放大效果:https://www.cnblogs.com/mingc/p/7446492.html 使用 1. 下载…

    数据库 2023年6月9日
    089
  • java中如何将函数作为参数传递呢?

    函数简介: 函数(function)的定义通常分为传统定义和近代定义,函数的两个定义本质是相同的,只是叙述概念的出发点不同,传统定义是从运动变化的观点出发,而近代定义是从集合、映射…

    数据库 2023年6月11日
    091
  • COM组件中 添加导出函数的方法

    COM组件中 添加导出函数的方法 0准备 类前缀 define ATL_NO_VTABLE __declspec(novtable) 函数前缀 define STDMETHODIM…

    数据库 2023年6月14日
    0102
  • 数据结构与算法-农夫过河问题

    农夫过河问题——最短路径算法 问题描述:农夫用小木筏将狼、羊、菜从起始岸运到目标岸,小木筏每次只能带一种物品,也可以什么都不带,因为食物链的关系,人不在的时候,狼会吃羊,羊会吃菜,…

    数据库 2023年6月14日
    0109
  • 一元二次方程

    通过分析古巴比伦泥板上的代数问题,可以发现在公元前2250年古巴比伦人就已经掌握了与求解一元二次方程相关的代数学知识,并将之应用于解决有关矩形面积和边的问题。 [2] 相关的算法可…

    数据库 2023年6月11日
    093
  • 多商户商城系统功能拆解23讲-平台端分销等级

    多商户商城系统,也称为B2B2C(BBC)平台电商模式多商家商城系统。可以快速帮助企业搭建类似拼多多/京东/天猫/淘宝的综合商城。 多商户商城系统支持商家入驻加盟,同时满足平台自营…

    数据库 2023年6月14日
    090
  • 【Java基础】– FileUtils工具类常用方法

    1.FileUtils介绍 文件IO是我们日常项目中经常使用到的基础API,常见的IO读写操作基础类字节流InputStream与OutputStream、字符流Reader与Wr…

    数据库 2023年6月6日
    0359
  • xtrabackup2版本和xtrabackup8版本对比

    导语在使用xtrabackup8版本对mysql8版本进行备份恢复搭建从库的时候,继续使用xtrabackup2版本的方式,从xtrabackup_binlog_info 文件中找…

    数据库 2023年6月16日
    0117
  • 【运维】– Docker基础必知必会(1)

    1.Docker简介 Docker的出现简单地说就是为了解决运行环境和软件配置相关的不一致性问题,采用Docker镜像的方式将软件所需要的运行环境打包。通过Docker build…

    数据库 2023年6月6日
    0111
  • MySQL常用语句

    数据库设置 查看设置 `sql Original: https://www.cnblogs.com/1fengchen1/p/15781973.htmlAuthor: SonnyZ…

    数据库 2023年6月9日
    071
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球