mysql基础视频 day01课堂学习笔记(参考老师的笔记)
计算机-->右键-->管理-->服务和应用程序-->服务-->找mysql服务
MySQL的服务,默认是“启动”的状态,只有启动了mysql才能用。
默认情况下是“自动”启动,自动启动表示下一次重启操作系统的时候自动启动该服务。
可以在服务上点击右键:
还可以改变服务的默认配置:
C:\Users\Administrator>mysql -uroot -p123456 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.5.36 MySQL Community Server (GPL) Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
C:\Users\Administrator>mysql -uroot -p Enter password: ****** Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.5.36 MySQL Community Server (GPL) Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+
mysql> use test; Database changed 表示正在使用一个名字叫做test的数据库。
mysql> create database bjpowernode; Query OK, 1 row affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | bjpowernode | | mysql | | performance_schema | | test | +--------------------+
查看某个数据库下有哪些表?:show tables;
注意:以上的命令不区分大小写,都行。
查看mysql数据库的版本号:select version();
查看当前使用的是哪个数据库?:select database();
注意:mysql是不见分号“;”不执行,“;”表示结束!
\c用来终止一条命令的输入。
姓名 性别 年龄(列:字段) --------------------------- 张三 男 20 ------->行(记录) 李四 女 21 ------->行(记录) 王五 男 22 ------->行(记录)
mysql>create datebase bjpowernode; mysql>use bjpowernode; mysql>source D:\course\03-MySQL\document\bjpowernode.sql
mysql> show tables; +-----------------------+ | Tables_in_bjpowernode | +-----------------------+ | dept | | emp | | salgrade | +-----------------------+ dept是部门表 emp是员工表 salgrade 是工资等级表
mysql> select dname from dept; +------------+ | dname | +------------+ | ACCOUNTING | | RESEARCH | | SALES | | OPERATIONS | +------------+ 4 rows in set (0.00 sec) mysql> SELECT DNAME FROM DEPT; +------------+ | DNAME | +------------+ | ACCOUNTING | | RESEARCH | | SALES | | OPERATIONS | +------------+ 4 rows in set (0.00 sec)
select deptno,dname from dept; +--------+------------+ | deptno | dname | +--------+------------+ | 10 | ACCOUNTING | | 20 | RESEARCH | | 30 | SALES | | 40 | OPERATIONS | +--------+------------+
select * from dept; +--------+------------+----------+ | DEPTNO | DNAME | LOC | +--------+------------+----------+ | 10 | ACCOUNTING | NEW YORK | | 20 | RESEARCH | DALLAS | | 30 | SALES | CHICAGO | | 40 | OPERATIONS | BOSTON | +--------+------------+----------+
mysql> select deptno,dname as deptname from dept; +--------+------------+ | deptno | deptname | +--------+------------+ | 10 | ACCOUNTING | | 20 | RESEARCH | | 30 | SALES | | 40 | OPERATIONS | +--------+------------+
mysql> select deptno,dname deptname from dept;
select deptno,dname 'dept name' from dept; //加单引号 select deptno,dname "dept name" from dept; //加双引号 +--------+------------+ | deptno | dept name | +--------+------------+ | 10 | ACCOUNTING | | 20 | RESEARCH | | 30 | SALES | | 40 | OPERATIONS | +--------+------------+
mysql> select ename,sal from emp; +--------+---------+ | ename | sal | +--------+---------+ | SMITH | 800.00 | | ALLEN | 1600.00 | | WARD | 1250.00 | | JONES | 2975.00 | | MARTIN | 1250.00 | | BLAKE | 2850.00 | | CLARK | 2450.00 | | SCOTT | 3000.00 | | KING | 5000.00 | | TURNER | 1500.00 | | ADAMS | 1100.00 | | JAMES | 950.00 | | FORD | 3000.00 | | MILLER | 1300.00 | +--------+---------+ mysql> 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 | +--------+----------+ mysql> select ename,sal*12 as yearsal from emp; //起别名 +--------+----------+ | ename | yearsal | +--------+----------+ | 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 | +--------+----------+ mysql> 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 | +--------+----------+
select 字段1,字段2,字段3.... from 表名 where 条件;(带某个字段的条件)
// 查询薪资等于800的员工姓名和编号? select empno,ename from emp where sal = 800; // 查询SMITH的编号和薪资? select empno,sal from emp where ename = 'SMITH'; //字符串使用单引号
// 查询薪资不等于800的员工姓名和编号? select empno,ename from emp where sal != 800; select empno,ename from emp where sal <> 800; // 小于号和大于号组成的不等号
// 查询薪资小于2000的员工姓名和编号? mysql> select empno,ename,sal from emp where sal < 2000; +-------+--------+---------+ | empno | ename | sal | +-------+--------+---------+ | 7369 | SMITH | 800.00 | | 7499 | ALLEN | 1600.00 | | 7521 | WARD | 1250.00 | | 7654 | MARTIN | 1250.00 | | 7844 | TURNER | 1500.00 | | 7876 | ADAMS | 1100.00 | | 7900 | JAMES | 950.00 | | 7934 | MILLER | 1300.00 | +-------+--------+---------+
// 查询薪资小于等于3000的员工姓名和编号? select empno,ename,sal from emp where sal <= 3000;
// 查询薪资大于3000的员工姓名和编号? select empno,ename,sal from emp where sal > 3000;
// 查询薪资大于等于3000的员工姓名和编号? select empno,ename,sal from emp where sal >= 3000;
// 查询薪资在2450和3000之间的员工信息?包括2450和3000 // 第一种方式:>= and <= (and是并且的意思。) select empno,ename,sal from emp where sal >= 2450 and sal <= 3000; +-------+-------+---------+ | empno | ename | sal | +-------+-------+---------+ | 7566 | JONES | 2975.00 | | 7698 | BLAKE | 2850.00 | | 7782 | CLARK | 2450.00 | | 7788 | SCOTT | 3000.00 | | 7902 | FORD | 3000.00 | +-------+-------+---------+ // 第二种方式:between … and … select empno,ename,sal from emp where sal between 2450 and 3000;
// 查询哪些员工的津贴/补助为null? mysql> select empno,ename,sal,comm from emp where comm = null; Empty set (0.00 sec) // 表示查不到任何数据,所以=null不能用 mysql> select empno,ename,sal,comm from emp where comm is null; +-------+--------+---------+------+ | empno | ename | sal | comm | +-------+--------+---------+------+ | 7369 | SMITH | 800.00 | NULL | | 7566 | JONES | 2975.00 | NULL | | 7698 | BLAKE | 2850.00 | NULL | | 7782 | CLARK | 2450.00 | NULL | | 7788 | SCOTT | 3000.00 | NULL | | 7839 | KING | 5000.00 | NULL | | 7876 | ADAMS | 1100.00 | NULL | | 7900 | JAMES | 950.00 | NULL | | 7902 | FORD | 3000.00 | NULL | | 7934 | MILLER | 1300.00 | NULL | +-------+--------+---------+------+ 10 rows in set (0.00 sec) // 查询哪些员工的津贴/补助不为null? mysql> select empno,ename,sal,comm from emp where comm is not null; +-------+--------+---------+---------+ | empno | ename | sal | comm | +-------+--------+---------+---------+ | 7499 | ALLEN | 1600.00 | 300.00 | | 7521 | WARD | 1250.00 | 500.00 | | 7654 | MARTIN | 1250.00 | 1400.00 | | 7844 | TURNER | 1500.00 | 0.00 | +-------+--------+---------+---------+
// 查询工作岗位是MANAGER并且工资大于2500的员工信息? select empno,ename,job,sal from emp where job = 'MANAGER' and sal > 2500; +-------+-------+---------+---------+ | empno | ename | job | sal | +-------+-------+---------+---------+ | 7566 | JONES | MANAGER | 2975.00 | | 7698 | BLAKE | MANAGER | 2850.00 | +-------+-------+---------+---------+
// 查询工作岗位是MANAGER和SALESMAN的员工? select empno,ename,job from emp where job = 'MANAGER'; select empno,ename,job from emp where job = 'SALESMAN'; select empno,ename,job from emp where job = 'MANAGER' or job = 'SALESMAN'; +-------+--------+----------+ | empno | ename | job | +-------+--------+----------+ | 7499 | ALLEN | SALESMAN | | 7521 | WARD | SALESMAN | | 7566 | JONES | MANAGER | | 7654 | MARTIN | SALESMAN | | 7698 | BLAKE | MANAGER | | 7782 | CLARK | MANAGER | | 7844 | TURNER | SALESMAN | +-------+--------+----------+
// 查询工资大于2500,并且部门编号为10或20部门的员工? select * from emp where sal > 2500 and deptno = 10 or deptno = 20; // 分析以上语句存在的问题? // and优先级比or高。以上语句会先执行and,然后执行or。 // 以上这个语句表示什么含义? // 找出工资大于2500并且部门编号为10的员工,或者20部门所有员工找出来。 // 加小括号: select * from emp where sal > 2500 and (deptno = 10 or deptno = 20);
// 查询工作岗位是MANAGER和SALESMAN的员工? select empno,ename,job from emp where job = 'MANAGER' or job = 'SALESMAN'; select empno,ename,job from emp where job in('MANAGER', 'SALESMAN'); +-------+--------+----------+ | empno | ename | job | +-------+--------+----------+ | 7499 | ALLEN | SALESMAN | | 7521 | WARD | SALESMAN | | 7566 | JONES | MANAGER | | 7654 | MARTIN | SALESMAN | | 7698 | BLAKE | MANAGER | | 7782 | CLARK | MANAGER | | 7844 | TURNER | SALESMAN | +-------+--------+----------+ // 查询薪资是800和5000的员工信息? select ename,sal from emp where sal = 800 or sal = 5000; select ename,sal from emp where sal in(800, 5000); //这个不是表示800到5000都找出来。 +-------+---------+ | ename | sal | +-------+---------+ | SMITH | 800.00 | | KING | 5000.00 | +-------+---------+ select ename,sal from emp where sal in(800, 5000, 3000); // not in 表示不在这几个值当中的数据。 select ename,sal from emp where sal not in(800, 5000, 3000); +--------+---------+ | ename | sal | +--------+---------+ | ALLEN | 1600.00 | | WARD | 1250.00 | | JONES | 2975.00 | | MARTIN | 1250.00 | | BLAKE | 2850.00 | | CLARK | 2450.00 | | TURNER | 1500.00 | | ADAMS | 1100.00 | | JAMES | 950.00 | | MILLER | 1300.00 | +--------+---------+
not 可以取非,主要用在 is 或 in 中
like 称为模糊查询,支持%或下划线匹配
// 找出名字中含有O的? mysql> select ename from emp where ename like '%O%'; +-------+ | ename | +-------+ | JONES | | SCOTT | | FORD | +-------+ // 找出名字以T结尾的? select ename from emp where ename like '%T'; // 找出名字以K开始的? select ename from emp where ename like 'K%'; // 找出第二个字每是A的? select ename from emp where ename like '_A%'; // 找出第三个字母是R的? select ename from emp where ename like '__R%'; // 找出t_student学生表中名字中有“_”的? select name from t_student where name like '%_%'; //这样不行。 mysql> select name from t_student where name like '%\_%'; // \转义字符。 +----------+ | name | +----------+ | jack_son | +----------+
// 查询所有员工薪资并排序? 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 | +--------+---------+
// 指定降序:desc select ename,sal from emp order by sal desc; +--------+---------+ | ename | sal | +--------+---------+ | KING | 5000.00 | | SCOTT | 3000.00 | | FORD | 3000.00 | | JONES | 2975.00 | | BLAKE | 2850.00 | | CLARK | 2450.00 | | ALLEN | 1600.00 | | TURNER | 1500.00 | | MILLER | 1300.00 | | MARTIN | 1250.00 | | WARD | 1250.00 | | ADAMS | 1100.00 | | JAMES | 950.00 | | SMITH | 800.00 | +--------+---------+ // 显式指定升序:asc select ename,sal from emp order by sal asc; +--------+---------+ | 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 | +--------+---------+
// 查询员工名字和薪资,要求按照薪资升序,如果薪资一样的话,再按照名字升序排列。 select ename,sal from emp order by sal asc, ename asc; // sal在前,起主导,只有sal相等的时候,才会考虑启用ename排序。 +--------+---------+ | ename | sal | +--------+---------+ | SMITH | 800.00 | | JAMES | 950.00 | | ADAMS | 1100.00 | | MARTIN | 1250.00 | | WARD | 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 | +--------+---------+
select ename,sal from emp order by 2; // 2表示第二列。第二列是sal按照查询结果的第2列sal排序。
// 找出工资在1250到3000之间的员工信息,要求按照薪资降序排列。 select ename,sal from emp where sal between 1250 and 3000 order by sal desc; +--------+---------+ | ename | sal | +--------+---------+ | FORD | 3000.00 | | SCOTT | 3000.00 | | JONES | 2975.00 | | BLAKE | 2850.00 | | CLARK | 2450.00 | | ALLEN | 1600.00 | | TURNER | 1500.00 | | MILLER | 1300.00 | | MARTIN | 1250.00 | | WARD | 1250.00 | +--------+---------+
mysql> select lower(ename) as ename from emp; +--------+ | ename | +--------+ | smith | | allen | | ward | | jones | | martin | | blake | | clark | | scott | | king | | turner | | adams | | james | | ford | | miller | +--------+ // 14个输入,最后还是14个输出。这是单行处理函数的特点。一行一行地处理 // 一般处理后用as起别名,不然就会直接显示名字是lower(ename) as ename
mysql> select * from t_student; +----------+ | name | +----------+ | zhangsan | | lisi | | wangwu | | jack_son | +----------+ mysql> select upper(name) as name from t_student; +----------+ | name | +----------+ | ZHANGSAN | | LISI | | WANGWU | | JACK_SON | +----------+
select substr(ename, 1, 1) as ename from emp;
// 找出员工名字第一个字母是A的员工信息? // 第一种方式:模糊查询 select ename from emp where ename like 'A%'; // 第二种方式:substr函数 select ename from emp where substr(ename,1,1) = 'A'; // 首字母大写? // 分步分析: select name from t_student; select upper(substr(name,1,1)) from t_student; select substr(name,2,length(name) - 1) from t_student; // 结合: select concat(upper(substr(name,1,1)),substr(name,2,length(name) - 1)) as result from t_student; +----------+ | result | +----------+ | Zhangsan | | Lisi | | Wangwu | | Jack_son | +----------+
select concat(empno,ename) from emp; +---------------------+ | concat(empno,ename) | +---------------------+ | 7369SMITH | | 7499ALLEN | | 7521WARD | | 7566JONES | | 7654MARTIN | | 7698BLAKE | | 7782CLARK | | 7788SCOTT | | 7839KING | | 7844TURNER | | 7876ADAMS | | 7900JAMES | | 7902FORD | | 7934MILLER | +---------------------+
select length(ename) enamelength from emp; +-------------+ | enamelength | +-------------+ | 5 | | 5 | | 4 | | 5 | | 6 | | 5 | | 5 | | 5 | | 4 | | 6 | | 5 | | 5 | | 4 | | 6 | +-------------+
mysql> select * from emp where ename = ' KING'; Empty set (0.00 sec) // 查不到数据 mysql> select * from emp where ename = trim(' KING'); +-------+-------+-----------+------+------------+---------+------+--------+ | EMPNO | ENAME | JOB | MGR | HIREDATE | SAL | COMM | DEPTNO | +-------+-------+-----------+------+------------+---------+------+--------+ | 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 5000.00 | NULL | 10 | +-------+-------+-----------+------+------------+---------+------+--------+
// 当员工的工作岗位是MANAGER的时候,工资上调10%,当工作岗位是SALESMAN的时候,工资上调50%,其它正常。 // 注意:不修改数据库,只是将查询结果显示为工资上调 select ename, job, sal as oldsal, (case job when 'MANAGER' then sal*1.1 when 'SALESMAN' then sal*1.5 else sal end) as newsal from emp; +--------+-----------+---------+---------+ | ename | job | oldsal | newsal | +--------+-----------+---------+---------+ | SMITH | CLERK | 800.00 | 800.00 | | ALLEN | SALESMAN | 1600.00 | 2400.00 | | WARD | SALESMAN | 1250.00 | 1875.00 | | JONES | MANAGER | 2975.00 | 3272.50 | | MARTIN | SALESMAN | 1250.00 | 1875.00 | | BLAKE | MANAGER | 2850.00 | 3135.00 | | CLARK | MANAGER | 2450.00 | 2695.00 | | SCOTT | ANALYST | 3000.00 | 3000.00 | | KING | PRESIDENT | 5000.00 | 5000.00 | | TURNER | SALESMAN | 1500.00 | 2250.00 | | ADAMS | CLERK | 1100.00 | 1100.00 | | JAMES | CLERK | 950.00 | 950.00 | | FORD | ANALYST | 3000.00 | 3000.00 | | MILLER | CLERK | 1300.00 | 1300.00 | +--------+-----------+---------+---------+
select 字段 from 表名; select ename from emp; select 'abc' from emp; // select后面直接跟“字面量/字面值” mysql> select 'abc' as bieming from emp; +---------+ | bieming | +---------+ | abc | | abc | | abc | | abc | | abc | | abc | | abc | | abc | | abc | | abc | | abc | | abc | | abc | | abc | +---------+ mysql> select abc from emp; ERROR 1054 (42S22): Unknown column 'abc' in 'field list' // 这样肯定报错,因为会把abc当做一个字段的名字,去emp表中找abc字段去了。 select 1000 as num from emp; // 1000 也是被当做一个字面量/字面值。 +------+ | num | +------+ | 1000 | | 1000 | | 1000 | | 1000 | | 1000 | | 1000 | | 1000 | | 1000 | | 1000 | | 1000 | | 1000 | | 1000 | | 1000 | | 1000 | +------+ select 21000 as num from dept; +-------+ | num | +-------+ | 21000 | | 21000 | | 21000 | | 21000 | +-------+
mysql> select round(1236.567, 0) as result from emp; //保留整数位。 +--------+ | result | +--------+ | 1237 | | 1237 | | 1237 | | 1237 | | 1237 | | 1237 | | 1237 | | 1237 | | 1237 | | 1237 | | 1237 | | 1237 | | 1237 | | 1237 | +--------+ select round(1236.567, 1) as result from emp; //保留1个小数 select round(1236.567, 2) as result from emp; //保留2个小数 select round(1236.567, -1) as result from emp; // 保留到十位。 +--------+ | result | +--------+ | 1240 | | 1240 | | 1240 | | 1240 | | 1240 | | 1240 | | 1240 | | 1240 | | 1240 | | 1240 | | 1240 | | 1240 | | 1240 | | 1240 | +--------+ select round(1236.567, -2) as result from emp; // 保留到百位。 +--------+ | result | +--------+ | 1200 | | 1200 | | 1200 | | 1200 | | 1200 | | 1200 | | 1200 | | 1200 | | 1200 | | 1200 | | 1200 | | 1200 | | 1200 | | 1200 | +--------+
// 下面的例子只是借助了表的结构输出 mysql> select round(rand()*100,0) from emp; // 100以内的随机数 +---------------------+ | round(rand()*100,0) | +---------------------+ | 76 | | 29 | | 15 | | 88 | | 95 | | 9 | | 63 | | 89 | | 54 | | 3 | | 54 | | 61 | | 42 | | 28 | +---------------------+
mysql> select ename, sal + comm as salcomm from emp; +--------+---------+ | ename | salcomm | +--------+---------+ | SMITH | NULL | | ALLEN | 1900.00 | | WARD | 1750.00 | | JONES | NULL | | MARTIN | 2650.00 | | BLAKE | NULL | | CLARK | NULL | | SCOTT | NULL | | KING | NULL | | TURNER | 1500.00 | | ADAMS | NULL | | JAMES | NULL | | FORD | NULL | | MILLER | NULL | +--------+---------+ // 计算每个员工的年薪?年薪 = (月薪 + 月补助) * 12 select ename, (sal + comm) * 12 as yearsal from emp; +--------+----------+ | ename | yearsal | +--------+----------+ | SMITH | NULL | | ALLEN | 22800.00 | | WARD | 21000.00 | | JONES | NULL | | MARTIN | 31800.00 | | BLAKE | NULL | | CLARK | NULL | | SCOTT | NULL | | KING | NULL | | TURNER | 18000.00 | | ADAMS | NULL | | JAMES | NULL | | FORD | NULL | | MILLER | NULL | +--------+----------+ // 注意:NULL只要参与运算,最终结果一定是NULL。为了避免这个现象,需要使用ifnull函数 // 补助为NULL的时候,将补助当做0 select ename, (sal + ifnull(comm, 0)) * 12 as yearsal from emp; +--------+----------+ | ename | yearsal | +--------+----------+ | SMITH | 9600.00 | | ALLEN | 22800.00 | | WARD | 21000.00 | | JONES | 35700.00 | | MARTIN | 31800.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 | +--------+----------+
多行处理函数的特点:输入多行,最终输出一行。
5个:
注意:
分组函数在使用的时候必须先进行分组,然后才能用。
如果你没有对数据进行分组,整张表默认为一组。
// 找出最高工资? mysql> select max(sal) from emp; +----------+ | max(sal) | +----------+ | 5000.00 | +----------+ // 找出最低工资? mysql> select min(sal) from emp; +----------+ | min(sal) | +----------+ | 800.00 | +----------+ // 计算平均工资: mysql> select avg(sal) from emp; +-------------+ | avg(sal) | +-------------+ | 2073.214286 | +-------------+ // 14个工资全部加起来,然后除以14。 // 计算员工数量? mysql> select count(ename) from emp; +--------------+ | count(ename) | +--------------+ | 14 | +--------------+
mysql> select sum(comm) from emp; +-----------+ | sum(comm) | +-----------+ | 2200.00 | +-----------+ mysql> select count(comm) from emp; +-------------+ | count(comm) | +-------------+ | 4 | +-------------+ mysql> select avg(comm) from emp; +------------+ | avg(comm) | +------------+ | 550.000000 | +------------+
mysql> select count(*) from emp; +----------+ | count(*) | +----------+ | 14 | +----------+ mysql> select count(comm) from emp; +-------------+ | count(comm) | +-------------+ | 4 | +-------------+
// 找出比最低工资高的员工信息。 select ename,sal from emp where sal > min(sal); // 表面上没问题,运行一下? ERROR 1111 (HY000): Invalid use of group function // 说完分组查询(group by)之后就明白了了。
select sum(sal),min(sal),max(sal),avg(sal),count(*) from emp; +----------+----------+----------+-------------+----------+ | sum(sal) | min(sal) | max(sal) | avg(sal) | count(*) | +----------+----------+----------+-------------+----------+ | 29025.00 | 800.00 | 5000.00 | 2073.214286 | 14 | +----------+----------+----------+-------------+----------+
select ... from ... group by ... 计算每个部门的工资和? 计算每个工作岗位的平均薪资? 找出每个工作岗位的最高薪资? ....
select ename,sal from emp where sal > min(sal);//报错。 // 分组函数在使用的时候必须先分组之后才能使用 select sum(sal) from emp; // 这个没有分组,为啥sum()函数可以用呢? // 因为select在group by之后执行。这里默认group by为整组
// 实现思路:按照工作岗位分组,然后对工资求和。 select job,sum(sal) from emp group by job; +-----------+----------+ | job | sum(sal) | +-----------+----------+ | ANALYST | 6000.00 | | CLERK | 4150.00 | | MANAGER | 8275.00 | | PRESIDENT | 5000.00 | | SALESMAN | 5600.00 | +-----------+----------+ // 以上这个语句的执行顺序? // 先从emp表中查询数据。 // 根据job字段进行分组。 // 然后对每一组的数据进行sum(sal) select ename,job,sum(sal) from emp group by job; +-------+-----------+----------+ | ename | job | sum(sal) | +-------+-----------+----------+ | SCOTT | ANALYST | 6000.00 | | SMITH | CLERK | 4150.00 | | JONES | MANAGER | 8275.00 | | KING | PRESIDENT | 5000.00 | | ALLEN | SALESMAN | 5600.00 | +-------+-----------+----------+ // 以上在select后面添加ename字段的语句在mysql中可以执行,但是毫无意义。 // 以上语句在oracle中执行报错。 // oracle的语法比mysql的语法严格。(mysql的语法相对来说松散一些!)
// 实现思路是什么?按照部门编号分组,求每一组的最大值 // select后面添加ename字段没有意义,另外oracle会报错。 mysql> select ename,deptno,max(sal) from emp group by deptno; +-------+--------+----------+ | ename | deptno | max(sal) | +-------+--------+----------+ | CLARK | 10 | 5000.00 | | SMITH | 20 | 3000.00 | | ALLEN | 30 | 2850.00 | +-------+--------+----------+ mysql> select deptno,max(sal) from emp group by deptno; +--------+----------+ | deptno | max(sal) | +--------+----------+ | 10 | 5000.00 | | 20 | 3000.00 | | 30 | 2850.00 | +--------+----------+
+--------+-----------+---------+--------+ | ename | job | sal | deptno | +--------+-----------+---------+--------+ | MILLER | CLERK | 1300.00 | 10 | | KING | PRESIDENT | 5000.00 | 10 | | CLARK | MANAGER | 2450.00 | 10 | | FORD | ANALYST | 3000.00 | 20 | | ADAMS | CLERK | 1100.00 | 20 | | SCOTT | ANALYST | 3000.00 | 20 | | JONES | MANAGER | 2975.00 | 20 | | SMITH | CLERK | 800.00 | 20 | | BLAKE | MANAGER | 2850.00 | 30 | | MARTIN | SALESMAN | 1250.00 | 30 | | ALLEN | SALESMAN | 1600.00 | 30 | | TURNER | SALESMAN | 1500.00 | 30 | | WARD | SALESMAN | 1250.00 | 30 | | JAMES | CLERK | 950.00 | 30 | +--------+-----------+---------+--------+ // 技巧:两个字段联合成1个字段看。(两个字段联合分组) select deptno, job, max(sal) from emp group by deptno, job; // 先分部门再分岗位 +--------+-----------+----------+ | deptno | job | max(sal) | +--------+-----------+----------+ | 10 | CLERK | 1300.00 | | 10 | MANAGER | 2450.00 | | 10 | PRESIDENT | 5000.00 | | 20 | ANALYST | 3000.00 | | 20 | CLERK | 1100.00 | | 20 | MANAGER | 2975.00 | | 30 | CLERK | 950.00 | | 30 | MANAGER | 2850.00 | | 30 | SALESMAN | 1600.00 | +--------+-----------+----------+
// 找出每个部门最高薪资,要求显示最高薪资大于3000的? // 第一步:找出每个部门最高薪资 // 按照部门编号分组,求每一组最大值。 按照部门编号分组,求每一组最大值。 select deptno,max(sal) from emp group by deptno; +--------+----------+ | deptno | max(sal) | +--------+----------+ | 10 | 5000.00 | | 20 | 3000.00 | | 30 | 2850.00 | +--------+----------+ // 第二步:要求显示最高薪资大于3000 select deptno,max(sal) from emp group by deptno having max(sal) > 3000; // 分完组后 +--------+----------+ | deptno | max(sal) | +--------+----------+ | 10 | 5000.00 | +--------+----------+ // 思考一个问题:以上的sql语句执行效率是不是低? // 比较低,实际上可以这样考虑:先将大于3000的都找出来,然后再分组。 select deptno,max(sal) from emp where sal > 3000 // 先筛选出大于3000的再分组 group by deptno; +--------+----------+ | deptno | max(sal) | +--------+----------+ | 10 | 5000.00 | +--------+----------+
// 找出每个部门平均薪资,要求显示平均薪资高于2500的。 // 第一步:找出每个部门平均薪资 select deptno,avg(sal) from emp group by deptno; +--------+-------------+ | deptno | avg(sal) | +--------+-------------+ | 10 | 2916.666667 | | 20 | 2175.000000 | | 30 | 1566.666667 | +--------+-------------+ // 第二步:要求显示平均薪资高于2500的 select deptno,avg(sal) from emp group by deptno having avg(sal) > 2500; +--------+-------------+ | deptno | avg(sal) | +--------+-------------+ | 10 | 2916.666667 | +--------+-------------+
select (5)select查询出每组数据 from (1)待查询的表 where (2)先经过where条件从表中筛选出有价值的数据 group by (3)对这些筛选出的数据进行分组 having (4)分完组之后可以使用having分别对每组数据继续筛选 order by (6)最后将查询到的数据排序输出 ...
// 找出每个岗位的平均薪资,要求显示平均薪资大于1500的,除MANAGER岗位之外,要求按照平均薪资降序排。 select job, avg(sal) as avgsal from emp where job <> 'MANAGER' group by job having avg(sal) > 1500 order by avgsal desc; 因为order是在select后面执行,所以用别名 +-----------+-------------+ | job | avgsal | +-----------+-------------+ | PRESIDENT | 5000.000000 | | ANALYST | 3000.000000 | +-----------+-------------+