【二】【SQL】去重表數據及分組聚合查詢

去重表數據

表的準備工作

去除表中重復的數據,重復的數據只留一份。

 
mysql> create table duplicate_table (-> id int,-> name varchar(20)-> );
Query OK, 0 rows affected (0.03 sec)mysql> insert into duplicate_table values-> (100,'aaa'),-> (100,'aaa'),-> (200,'bbb'),-> (200,'bbb'),-> (200,'bbb'),-> (300,'ccc');
Query OK, 6 rows affected (0.01 sec)
Records: 6  Duplicates: 0  Warnings: 0mysql> select *from duplicate_table;
+------+------+
| id   | name |
+------+------+
|  100 | aaa  |
|  100 | aaa  |
|  200 | bbb  |
|  200 | bbb  |
|  200 | bbb  |
|  300 | ccc  |
+------+------+
6 rows in set (0.00 sec)mysql> 

去除表中重復的數據,重復的數據只留一份

 
mysql> create table no_duplicate_table like duplicate_table;
Query OK, 0 rows affected (0.03 sec)mysql> desc no_duplicate_table;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  |     | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)mysql> desc duplicate_table;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  |     | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)mysql> select distinct *from no_duplicate_table ;
Empty set (0.00 sec)mysql> select distinct *from duplicate_table ;
+------+------+
| id   | name |
+------+------+
|  100 | aaa  |
|  200 | bbb  |
|  300 | ccc  |
+------+------+
3 rows in set (0.00 sec)mysql> insert into no_duplicate_table select distinct *from duplicate_table;
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0mysql> select *from no_duplicate_table;
+------+------+
| id   | name |
+------+------+
|  100 | aaa  |
|  200 | bbb  |
|  300 | ccc  |
+------+------+
3 rows in set (0.00 sec)mysql> rename table duplicate_table to old_duplicate_table ;
Query OK, 0 rows affected (0.04 sec)mysql> rename table no_duplicate_table to duplicate_table ;
Query OK, 0 rows affected (0.01 sec)mysql> select *from duplicate_table ;
+------+------+
| id   | name |
+------+------+
|  100 | aaa  |
|  200 | bbb  |
|  300 | ccc  |
+------+------+
3 rows in set (0.00 sec)mysql> 

聚合統計

函數

說明

COUNT(DISTINCT)

返回查詢到的數據的 數量

SUM(DISTINCT)

返回查詢到的數據的 總和,不是數字沒有意義

AVG(DISTINCT)

返回查詢到的數據的 平均值,不是數字沒有意義

MAX(DISTINCT)

返回查詢到的數據的 最大值,不是數字沒有意義

MIN(DISTINCT)

返回查詢到的數據的 最小值,不是數字沒有意義

count簡單使用

 
mysql> select *from exam_result;
+----+-----------+---------+------+---------+
| id | name      | chinese | math | english |
+----+-----------+---------+------+---------+
|  1 | 唐三藏    |     134 |   98 |      56 |
|  3 | 豬悟能    |     176 |   98 |      90 |
|  4 | 曹孟德    |     140 |   90 |      67 |
|  6 | 孫權      |     140 |   73 |      78 |
|  7 | 宋公明    |     150 |   95 |      30 |
+----+-----------+---------+------+---------+
5 rows in set (0.00 sec)mysql> select count(*) from exam_result;
+----------+
| count(*) |
+----------+
|        5 |
+----------+
1 row in set (0.00 sec)mysql> select count(1) 總數 from exam_result;
+--------+
| 總數   |
+--------+
|      5 |
+--------+
1 row in set (0.00 sec)mysql> select count(2) 總數 from exam_result;
+--------+
| 總數   |
+--------+
|      5 |
+--------+
1 row in set (0.00 sec)mysql> select math from exam_result;
+------+
| math |
+------+
|   98 |
|   98 |
|   90 |
|   73 |
|   95 |
+------+
5 rows in set (0.00 sec)mysql> select count(math) from exam_result;
+-------------+
| count(math) |
+-------------+
|           5 |
+-------------+
1 row in set (0.00 sec)mysql> select count(math) as res from exam_result;
+-----+
| res |
+-----+
|   5 |
+-----+
1 row in set (0.00 sec)mysql> 

統計本次考試的數學成績分數個數

 
mysql> select math from exam_result;
+------+
| math |
+------+
|   98 |
|   98 |
|   90 |
|   73 |
|   95 |
+------+
5 rows in set (0.00 sec)mysql> select distinct count(math) as res from exam_result;
+-----+
| res |
+-----+
|   5 |
+-----+
1 row in set (0.01 sec)mysql> select count(distinct math) as res from exam_result;
+-----+
| res |
+-----+
|   4 |
+-----+
1 row in set (0.00 sec)mysql> 

數學英語平均分,不及格個數,不及格總分,不及格平均分

 
mysql> select sum(math) from exam_result;
+-----------+
| sum(math) |
+-----------+
|       454 |
+-----------+
1 row in set (0.00 sec)mysql> select sum(math)/count(*) from exam_result;
+--------------------+
| sum(math)/count(*) |
+--------------------+
|               90.8 |
+--------------------+
1 row in set (0.00 sec)mysql> select sum(english)/count(*) from exam_result;
+-----------------------+
| sum(english)/count(*) |
+-----------------------+
|                  64.2 |
+-----------------------+
1 row in set (0.00 sec)mysql> select count(*) from exam_result where math<60;
+----------+
| count(*) |
+----------+
|        0 |
+----------+
1 row in set (0.00 sec)mysql> select count(*) from exam_result where english<60;
+----------+
| count(*) |
+----------+
|        2 |
+----------+
1 row in set (0.00 sec)mysql> select *from exam_result where english<60;
+----+-----------+---------+------+---------+
| id | name      | chinese | math | english |
+----+-----------+---------+------+---------+
|  1 | 唐三藏    |     134 |   98 |      56 |
|  7 | 宋公明    |     150 |   95 |      30 |
+----+-----------+---------+------+---------+
2 rows in set (0.00 sec)mysql> select sum(english) from exam_result where english<60;
+--------------+
| sum(english) |
+--------------+
|           86 |
+--------------+
1 row in set (0.00 sec)mysql> select sum(english)/count(english) from exam_result where english<60;
+-----------------------------+
| sum(english)/count(english) |
+-----------------------------+
|                          43 |
+-----------------------------+
1 row in set (0.00 sec)mysql> 

統計平均總分

 
mysql> select sum(math)/count(*) from exam_result;
+--------------------+
| sum(math)/count(*) |
+--------------------+
|               90.8 |
+--------------------+
1 row in set (0.00 sec)mysql> select avg(math) from exam_result;
+-----------+
| avg(math) |
+-----------+
|      90.8 |
+-----------+
1 row in set (0.00 sec)mysql> select avg(math+chinese+english) from exam_result;
+---------------------------+
| avg(math+chinese+english) |
+---------------------------+
|                       303 |
+---------------------------+
1 row in set (0.00 sec)mysql> select name,math+english+chinese from exam_result;
+-----------+----------------------+
| name      | math+english+chinese |
+-----------+----------------------+
| 唐三藏    |                  288 |
| 豬悟能    |                  364 |
| 曹孟德    |                  297 |
| 孫權      |                  291 |
| 宋公明    |                  275 |
+-----------+----------------------+
5 rows in set (0.00 sec)mysql> select max(english) from exam_result;
+--------------+
| max(english) |
+--------------+
|           90 |
+--------------+
1 row in set (0.00 sec)mysql> select name ,max(english) from exam_result;
ERROR 1140 (42000): In aggregated query without GROUP BY, expression #1 of SELECT list containode=only_full_group_by
mysql> 

返回>70分以上的數學最低分

 
mysql> select math from exam_result;
+------+
| math |
+------+
|   98 |
|   98 |
|   90 |
|   73 |
|   95 |
+------+
5 rows in set (0.00 sec)mysql> select math from exam_result where math>70;
+------+
| math |
+------+
|   98 |
|   98 |
|   90 |
|   73 |
|   95 |
+------+
5 rows in set (0.00 sec)mysql> select min(math) from exam_result where math>70;
+-----------+
| min(math) |
+-----------+
|        73 |
+-----------+
1 row in set (0.00 sec)mysql> 

分組聚合查詢

導入數據庫樣例sql文件

scott_data.sql(文件名)(下面是文件內容)

      
DROP database IF EXISTS `scott`;
CREATE database IF NOT EXISTS `scott` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;USE `scott`;DROP TABLE IF EXISTS `dept`;
CREATE TABLE `dept` (`deptno` int(2) unsigned zerofill NOT NULL COMMENT '部門編號',`dname` varchar(14) DEFAULT NULL COMMENT '部門名稱',`loc` varchar(13) DEFAULT NULL COMMENT '部門所在地點'
);DROP TABLE IF EXISTS `emp`;
CREATE TABLE `emp` (`empno` int(6) unsigned zerofill NOT NULL COMMENT '雇員編號',`ename` varchar(10) DEFAULT NULL COMMENT '雇員姓名',`job` varchar(9) DEFAULT NULL COMMENT '雇員職位',`mgr` int(4) unsigned zerofill DEFAULT NULL COMMENT '雇員領導編號',`hiredate` datetime DEFAULT NULL COMMENT '雇傭時間',`sal` decimal(7,2) DEFAULT NULL COMMENT '工資月薪',`comm` decimal(7,2) DEFAULT NULL COMMENT '獎金',`deptno` int(2) unsigned zerofill DEFAULT NULL COMMENT '部門編號'
);DROP TABLE IF EXISTS `salgrade`;
CREATE TABLE `salgrade` (`grade` int(11) DEFAULT NULL COMMENT '等級',`losal` int(11) DEFAULT NULL COMMENT '此等級最低工資',`hisal` int(11) DEFAULT NULL COMMENT '此等級最高工資'
);insert into dept (deptno, dname, loc)
values (10, 'ACCOUNTING', 'NEW YORK');
insert into dept (deptno, dname, loc)
values (20, 'RESEARCH', 'DALLAS');
insert into dept (deptno, dname, loc)
values (30, 'SALES', 'CHICAGO');
insert into dept (deptno, dname, loc)
values (40, 'OPERATIONS', 'BOSTON');insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7369, 'SMITH', 'CLERK', 7902, '1980-12-17', 800, null, 20);insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7499, 'ALLEN', 'SALESMAN', 7698, '1981-02-20', 1600, 300, 30);insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7521, 'WARD', 'SALESMAN', 7698, '1981-02-22', 1250, 500, 30);insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7566, 'JONES', 'MANAGER', 7839, '1981-04-02', 2975, null, 20);insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7654, 'MARTIN', 'SALESMAN', 7698, '1981-09-28', 1250, 1400, 30);insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7698, 'BLAKE', 'MANAGER', 7839, '1981-05-01', 2850, null, 30);insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7782, 'CLARK', 'MANAGER', 7839, '1981-06-09', 2450, null, 10);insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7788, 'SCOTT', 'ANALYST', 7566, '1987-04-19', 3000, null, 20);insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7839, 'KING', 'PRESIDENT', null, '1981-11-17', 5000, null, 10);insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7844, 'TURNER', 'SALESMAN', 7698,'1981-09-08', 1500, 0, 30);insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7876, 'ADAMS', 'CLERK', 7788, '1987-05-23', 1100, null, 20);insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7900, 'JAMES', 'CLERK', 7698, '1981-12-03', 950, null, 30);insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7902, 'FORD', 'ANALYST', 7566, '1981-12-03', 3000, null, 20);insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7934, 'MILLER', 'CLERK', 7782, '1982-01-23', 1300, null, 10);insert into salgrade (grade, losal, hisal) values (1, 700, 1200);
insert into salgrade (grade, losal, hisal) values (2, 1201, 1400);
insert into salgrade (grade, losal, hisal) values (3, 1401, 2000);
insert into salgrade (grade, losal, hisal) values (4, 2001, 3000);
insert into salgrade (grade, losal, hisal) values (5, 3001, 9999);

?rz選擇scott_data.sql文件導入數據

[root@VM-8-12-centos d1]# cd /var/lib/mysql
[root@VM-8-12-centos mysql]# ls
auto.cnf         d1              ibtmp1              private_key.pem
ca-key.pem       ib_buffer_pool  mysql               public_key.pem
ca.pem           ibdata1         mysql.sock          server-cert.pem
client-cert.pem  ib_logfile0     mysql.sock.lock     server-key.pem
client-key.pem   ib_logfile1     performance_schema  sys
[root@VM-8-12-centos mysql]# rz     [root@VM-8-12-centos mysql]# ls
auto.cnf         d1              ibtmp1              private_key.pem  sys
ca-key.pem       ib_buffer_pool  mysql               public_key.pem
ca.pem           ibdata1         mysql.sock          scott_data.sql
client-cert.pem  ib_logfile0     mysql.sock.lock     server-cert.pem
client-key.pem   ib_logfile1     performance_schema  server-key.pem
 
mysql> source /var/lib/mysql/scott_data.sql;
Query OK, 0 rows affected, 1 warning (0.00 sec)Query OK, 1 row affected (0.00 sec)Database changed
Query OK, 0 rows affected, 1 warning (0.00 sec)Query OK, 0 rows affected (0.03 sec)Query OK, 0 rows affected, 1 warning (0.00 sec)Query OK, 0 rows affected (0.02 sec)Query OK, 0 rows affected, 1 warning (0.00 sec)Query OK, 0 rows affected (0.03 sec)Query OK, 1 row affected (0.00 sec)Query OK, 1 row affected (0.00 sec)Query OK, 1 row affected (0.01 sec)Query OK, 1 row affected (0.00 sec)Query OK, 1 row affected (0.01 sec)Query OK, 1 row affected (0.00 sec)Query OK, 1 row affected (0.00 sec)Query OK, 1 row affected (0.00 sec)Query OK, 1 row affected (0.01 sec)Query OK, 1 row affected (0.00 sec)Query OK, 1 row affected (0.00 sec)Query OK, 1 row affected (0.01 sec)Query OK, 1 row affected (0.00 sec)Query OK, 1 row affected (0.00 sec)Query OK, 1 row affected (0.00 sec)Query OK, 1 row affected (0.01 sec)Query OK, 1 row affected (0.00 sec)Query OK, 1 row affected (0.00 sec)Query OK, 1 row affected (0.01 sec)Query OK, 1 row affected (0.00 sec)Query OK, 1 row affected (0.02 sec)Query OK, 1 row affected (0.00 sec)Query OK, 1 row affected (0.00 sec)mysql> show tables;
+-----------------+
| Tables_in_scott |
+-----------------+
| dept            |
| emp             |
| salgrade        |
+-----------------+
3 rows in set (0.01 sec)mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| d1                 |
| mysql              |
| performance_schema |
| scott              |
| sys                |
+--------------------+
6 rows in set (0.00 sec)mysql> 

展示樣例sql數據表

 
mysql> show tables;
+-----------------+
| Tables_in_scott |
+-----------------+
| dept            |
| emp             |
| salgrade        |
+-----------------+
3 rows in set (0.00 sec)mysql> desc dept;
+--------+--------------------------+------+-----+---------+-------+
| Field  | Type                     | Null | Key | Default | Extra |
+--------+--------------------------+------+-----+---------+-------+
| deptno | int(2) unsigned zerofill | NO   |     | NULL    |       |
| dname  | varchar(14)              | YES  |     | NULL    |       |
| loc    | varchar(13)              | YES  |     | NULL    |       |
+--------+--------------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)mysql> select* from dept;
+--------+------------+----------+
| deptno | dname      | loc      |
+--------+------------+----------+
|     10 | ACCOUNTING | NEW YORK |
|     20 | RESEARCH   | DALLAS   |
|     30 | SALES      | CHICAGO  |
|     40 | OPERATIONS | BOSTON   |
+--------+------------+----------+
4 rows in set (0.00 sec)mysql> desc emp;
+----------+--------------------------+------+-----+---------+-------+
| Field    | Type                     | Null | Key | Default | Extra |
+----------+--------------------------+------+-----+---------+-------+
| empno    | int(6) unsigned zerofill | NO   |     | NULL    |       |
| ename    | varchar(10)              | YES  |     | NULL    |       |
| job      | varchar(9)               | YES  |     | NULL    |       |
| mgr      | int(4) unsigned zerofill | YES  |     | NULL    |       |
| hiredate | datetime                 | YES  |     | NULL    |       |
| sal      | decimal(7,2)             | YES  |     | NULL    |       |
| comm     | decimal(7,2)             | YES  |     | NULL    |       |
| deptno   | int(2) unsigned zerofill | YES  |     | NULL    |       |
+----------+--------------------------+------+-----+---------+-------+
8 rows in set (0.00 sec)mysql> select*from emp;
+--------+--------+-----------+------+---------------------+---------+---------+--------+
| empno  | ename  | job       | mgr  | hiredate            | sal     | comm    | deptno |
+--------+--------+-----------+------+---------------------+---------+---------+--------+
| 007369 | SMITH  | CLERK     | 7902 | 1980-12-17 00:00:00 |  800.00 |    NULL |     20 |
| 007499 | ALLEN  | SALESMAN  | 7698 | 1981-02-20 00:00:00 | 1600.00 |  300.00 |     30 |
| 007521 | WARD   | SALESMAN  | 7698 | 1981-02-22 00:00:00 | 1250.00 |  500.00 |     30 |
| 007566 | JONES  | MANAGER   | 7839 | 1981-04-02 00:00:00 | 2975.00 |    NULL |     20 |
| 007654 | MARTIN | SALESMAN  | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 |     30 |
| 007698 | BLAKE  | MANAGER   | 7839 | 1981-05-01 00:00:00 | 2850.00 |    NULL |     30 |
| 007782 | CLARK  | MANAGER   | 7839 | 1981-06-09 00:00:00 | 2450.00 |    NULL |     10 |
| 007788 | SCOTT  | ANALYST   | 7566 | 1987-04-19 00:00:00 | 3000.00 |    NULL |     20 |
| 007839 | KING   | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000.00 |    NULL |     10 |
| 007844 | TURNER | SALESMAN  | 7698 | 1981-09-08 00:00:00 | 1500.00 |    0.00 |     30 |
| 007876 | ADAMS  | CLERK     | 7788 | 1987-05-23 00:00:00 | 1100.00 |    NULL |     20 |
| 007900 | JAMES  | CLERK     | 7698 | 1981-12-03 00:00:00 |  950.00 |    NULL |     30 |
| 007902 | FORD   | ANALYST   | 7566 | 1981-12-03 00:00:00 | 3000.00 |    NULL |     20 |
| 007934 | MILLER | CLERK     | 7782 | 1982-01-23 00:00:00 | 1300.00 |    NULL |     10 |
+--------+--------+-----------+------+---------------------+---------+---------+--------+
14 rows in set (0.00 sec)mysql> desc salgrade;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| grade | int(11) | YES  |     | NULL    |       |
| losal | int(11) | YES  |     | NULL    |       |
| hisal | int(11) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
3 rows in set (0.00 sec)mysql> select *from salgrade;
+-------+-------+-------+
| grade | losal | hisal |
+-------+-------+-------+
|     1 |   700 |  1200 |
|     2 |  1201 |  1400 |
|     3 |  1401 |  2000 |
|     4 |  2001 |  3000 |
|     5 |  3001 |  9999 |
+-------+-------+-------+
5 rows in set (0.00 sec)mysql> 

如何顯示每個部門的平均工資和最高工資

 
mysql> select max(sal) 最高,avg(sal) 平均 from emp;
+---------+-------------+
| 最高    | 平均        |
+---------+-------------+
| 5000.00 | 2073.214286 |
+---------+-------------+
1 row in set (0.00 sec)mysql> select max(sal) 最高,avg(sal) 平均 from emp group by deptno;
+---------+-------------+
| 最高    | 平均        |
+---------+-------------+
| 5000.00 | 2916.666667 |
| 3000.00 | 2175.000000 |
| 2850.00 | 1566.666667 |
+---------+-------------+
3 rows in set (0.00 sec)mysql> select deptno,max(sal) 最高,avg(sal) 平均 from emp group by deptno;
+--------+---------+-------------+
| deptno | 最高    | 平均        |
+--------+---------+-------------+
|     10 | 5000.00 | 2916.666667 |
|     20 | 3000.00 | 2175.000000 |
|     30 | 2850.00 | 1566.666667 |
+--------+---------+-------------+
3 rows in set (0.00 sec)mysql> 

顯示每個部門的每種崗位的平均工資和最低工資

 
mysql> select *from emp;
+--------+--------+-----------+------+---------------------+---------+---------+--------+
| empno  | ename  | job       | mgr  | hiredate            | sal     | comm    | deptno |
+--------+--------+-----------+------+---------------------+---------+---------+--------+
| 007369 | SMITH  | CLERK     | 7902 | 1980-12-17 00:00:00 |  800.00 |    NULL |     20 |
| 007499 | ALLEN  | SALESMAN  | 7698 | 1981-02-20 00:00:00 | 1600.00 |  300.00 |     30 |
| 007521 | WARD   | SALESMAN  | 7698 | 1981-02-22 00:00:00 | 1250.00 |  500.00 |     30 |
| 007566 | JONES  | MANAGER   | 7839 | 1981-04-02 00:00:00 | 2975.00 |    NULL |     20 |
| 007654 | MARTIN | SALESMAN  | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 |     30 |
| 007698 | BLAKE  | MANAGER   | 7839 | 1981-05-01 00:00:00 | 2850.00 |    NULL |     30 |
| 007782 | CLARK  | MANAGER   | 7839 | 1981-06-09 00:00:00 | 2450.00 |    NULL |     10 |
| 007788 | SCOTT  | ANALYST   | 7566 | 1987-04-19 00:00:00 | 3000.00 |    NULL |     20 |
| 007839 | KING   | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000.00 |    NULL |     10 |
| 007844 | TURNER | SALESMAN  | 7698 | 1981-09-08 00:00:00 | 1500.00 |    0.00 |     30 |
| 007876 | ADAMS  | CLERK     | 7788 | 1987-05-23 00:00:00 | 1100.00 |    NULL |     20 |
| 007900 | JAMES  | CLERK     | 7698 | 1981-12-03 00:00:00 |  950.00 |    NULL |     30 |
| 007902 | FORD   | ANALYST   | 7566 | 1981-12-03 00:00:00 | 3000.00 |    NULL |     20 |
| 007934 | MILLER | CLERK     | 7782 | 1982-01-23 00:00:00 | 1300.00 |    NULL |     10 |
+--------+--------+-----------+------+---------------------+---------+---------+--------+
14 rows in set (0.00 sec)mysql> select deptno,avg(sal) 平均,min(sal) 最低 from emp group by deptno,job;
+--------+-------------+---------+
| deptno | 平均        | 最低    |
+--------+-------------+---------+
|     10 | 1300.000000 | 1300.00 |
|     10 | 2450.000000 | 2450.00 |
|     10 | 5000.000000 | 5000.00 |
|     20 | 3000.000000 | 3000.00 |
|     20 |  950.000000 |  800.00 |
|     20 | 2975.000000 | 2975.00 |
|     30 |  950.000000 |  950.00 |
|     30 | 2850.000000 | 2850.00 |
|     30 | 1400.000000 | 1250.00 |
+--------+-------------+---------+
9 rows in set (0.00 sec)mysql> select deptno,job, avg(sal) 平均,min(sal) 最低 from emp group by deptno,job;
+--------+-----------+-------------+---------+
| deptno | job       | 平均        | 最低    |
+--------+-----------+-------------+---------+
|     10 | CLERK     | 1300.000000 | 1300.00 |
|     10 | MANAGER   | 2450.000000 | 2450.00 |
|     10 | PRESIDENT | 5000.000000 | 5000.00 |
|     20 | ANALYST   | 3000.000000 | 3000.00 |
|     20 | CLERK     |  950.000000 |  800.00 |
|     20 | MANAGER   | 2975.000000 | 2975.00 |
|     30 | CLERK     |  950.000000 |  950.00 |
|     30 | MANAGER   | 2850.000000 | 2850.00 |
|     30 | SALESMAN  | 1400.000000 | 1250.00 |
+--------+-----------+-------------+---------+
9 rows in set (0.01 sec)mysql> select ename,deptno,job, avg(sal) 平均,min(sal) 最低 from emp group by deptno,job;
ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'scott.emp.ename' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
mysql> 

顯示平均工資低于2000的部門和它的平均工資

 
mysql> select avg(sal) deptavg from emp group by deptno;
+-------------+
| deptavg     |
+-------------+
| 2916.666667 |
| 2175.000000 |
| 1566.666667 |
+-------------+
3 rows in set (0.00 sec)mysql> select deptno,avg(sal) deptavg from emp group by deptno;
+--------+-------------+
| deptno | deptavg     |
+--------+-------------+
|     10 | 2916.666667 |
|     20 | 2175.000000 |
|     30 | 1566.666667 |
+--------+-------------+
3 rows in set (0.00 sec)mysql> select deptno,avg(sal) deptavg from emp group by deptno having deptavg<2000;
+--------+-------------+
| deptno | deptavg     |
+--------+-------------+
|     30 | 1566.666667 |
+--------+-------------+
1 row in set (0.00 sec)mysql> select deptno,avg(sal) deptavg from emp group by deptno where deptavg<2000;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where deptavg<2000' at line 1
mysql> select *from emp having ename='SMITH';
+--------+-------+-------+------+---------------------+--------+------+--------+
| empno  | ename | job   | mgr  | hiredate            | sal    | comm | deptno |
+--------+-------+-------+------+---------------------+--------+------+--------+
| 007369 | SMITH | CLERK | 7902 | 1980-12-17 00:00:00 | 800.00 | NULL |     20 |
+--------+-------+-------+------+---------------------+--------+------+--------+
1 row in set (0.00 sec)mysql> 

(SMITH不參加統計)顯示平均工資低于2000的部門和它的平均工資

 
mysql> select deptno, job from emp where ename!='SMITH' group by deptno ,job;
+--------+-----------+
| deptno | job       |
+--------+-----------+
|     10 | CLERK     |
|     10 | MANAGER   |
|     10 | PRESIDENT |
|     20 | ANALYST   |
|     20 | CLERK     |
|     20 | MANAGER   |
|     30 | CLERK     |
|     30 | MANAGER   |
|     30 | SALESMAN  |
+--------+-----------+
9 rows in set (0.00 sec)mysql> select deptno, job ,max(sal) 最高,min(sal) 最低 from emp where ename!='SMITH' group by deptno ,job;
+--------+-----------+---------+---------+
| deptno | job       | 最高    | 最低    |
+--------+-----------+---------+---------+
|     10 | CLERK     | 1300.00 | 1300.00 |
|     10 | MANAGER   | 2450.00 | 2450.00 |
|     10 | PRESIDENT | 5000.00 | 5000.00 |
|     20 | ANALYST   | 3000.00 | 3000.00 |
|     20 | CLERK     | 1100.00 | 1100.00 |
|     20 | MANAGER   | 2975.00 | 2975.00 |
|     30 | CLERK     |  950.00 |  950.00 |
|     30 | MANAGER   | 2850.00 | 2850.00 |
|     30 | SALESMAN  | 1600.00 | 1250.00 |
+--------+-----------+---------+---------+
9 rows in set (0.00 sec)mysql> select deptno, job ,avg(sal) myavg from emp where ename!='SMITH' group by deptno ,job;
+--------+-----------+-------------+
| deptno | job       | myavg       |
+--------+-----------+-------------+
|     10 | CLERK     | 1300.000000 |
|     10 | MANAGER   | 2450.000000 |
|     10 | PRESIDENT | 5000.000000 |
|     20 | ANALYST   | 3000.000000 |
|     20 | CLERK     | 1100.000000 |
|     20 | MANAGER   | 2975.000000 |
|     30 | CLERK     |  950.000000 |
|     30 | MANAGER   | 2850.000000 |
|     30 | SALESMAN  | 1400.000000 |
+--------+-----------+-------------+
9 rows in set (0.00 sec)mysql> select deptno, job ,avg(sal) myavg from emp where ename!='SMITH' group by deptno ,job having mysal<2000;
ERROR 1054 (42S22): Unknown column 'mysal' in 'having clause'
mysql> select deptno, job ,avg(sal) myavg from emp where ename!='SMITH' group by deptno ,job having myavg<2000;
+--------+----------+-------------+
| deptno | job      | myavg       |
+--------+----------+-------------+
|     10 | CLERK    | 1300.000000 |
|     20 | CLERK    | 1100.000000 |
|     30 | CLERK    |  950.000000 |
|     30 | SALESMAN | 1400.000000 |
+--------+----------+-------------+
4 rows in set (0.00 sec)mysql> 

結尾

最后,感謝您閱讀我的文章,希望這些內容能夠對您有所啟發和幫助。如果您有任何問題或想要分享您的觀點,請隨時在評論區留言。

同時,不要忘記訂閱我的博客以獲取更多有趣的內容。在未來的文章中,我將繼續探討這個話題的不同方面,為您呈現更多深度和見解。

謝謝您的支持,期待與您在下一篇文章中再次相遇!

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/716065.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/716065.shtml
英文地址,請注明出處:http://en.pswp.cn/news/716065.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Day24-yum與rpm軟件包管理2

Day24-yum與rpm軟件包管理2 1. 配置緩存rpm包2. 為什么要緩存&#xff1f;3. 組包相關指令4. yum幫助與補全功能4.1 補全4.2 什么是yum源4.3 常見互聯網 yum 源 5. 搭建局域網YUM倉庫實踐 1. 配置緩存rpm包 修改yum.conf配置 [rootoldboy ~]# sed -i.bak s#keepcache0#keepca…

SLAM基礎知識:前端和后端

在基于濾波的SLAM算法中&#xff0c;使用迭代卡爾曼濾波&#xff08;Iterative Kalman Filtering&#xff09;來求解當前幀狀態量的步驟通常屬于SLAM系統的前端部分。 前端負責處理傳感器數據&#xff0c;進行狀態估計和地圖構建的初步步驟。迭代卡爾曼濾波作為一種濾波器&…

批次大小對ES寫入性能影響初探

問題背景 ES使用bulk寫入時每批次的大小對性能有什么影響&#xff1f;設置每批次多大為好&#xff1f; 一般來說&#xff0c;在Elasticsearch中&#xff0c;使用bulk API進行批量寫入時&#xff0c;每批次的大小對性能有著顯著的影響。具體來說&#xff0c;當批量請求的大小增…

PVLAN組網實驗

一&#xff0c;PVLAN類型 主VLAN 主VLAN可以由多個輔助私用VLAN組成&#xff0c;而這些輔VLAN與主VLAN屬于同一子網。 輔助VLAN ① 團體VLAN&#xff1a;如果某個端口屬于團體VLAN&#xff0c;那么它就不僅能夠與相同團體VLAN中的其他端口進行通信&#xff0c;而且還能夠與…

使用rsync同步服務器和客戶端的文件夾

使用rsync同步服務器和客戶端的文件夾 實現目的實驗準備實驗操作步驟服務器操作關閉防火墻和SELINUX安裝rsync修改服務器配置文件/etc/rsync.conf創建服務器備份文件的目錄創建rsync系統運行的用戶修改備份文件的所有者和所屬組創建rsync.passwd啟動rsync服務并進行驗證 客戶端…

中間件安全(概述)有中間件的各類鏈接和官網信息和漏洞庫以及配置問題和開源工具

分類主要包括Apache、IIS、Tomcat、weblogic、websphere、Jboss等相關的技術知識和實踐。 以Apache為例講一講如何保證中間件安全 中間件安全是指保護中間件軟件和服務的安全性&#xff0c;防止被惡意攻擊或者濫用。中間件軟件是指在操作系統和應用程序之間提供通信和集成功能…

【Go】命令行相關

查看go的環境 go env # 查看go的環境變量 goRoot # 編譯器的環境 goPath設置go module 打開cmd命令行&#xff0c;執行以下命令 go env -w GO111MODULEoff # on-打開 off-關閉 auto-自動相關命令 go build # 項目路徑下執行&#xff0c;能編譯當前go項目&#xff08;一個…

RFID(Radio Frequency Identification)技術筆記

一、RFID的介紹 RFID&#xff0c;全稱為Radio Frequency Identification&#xff0c;即射頻識別技術&#xff0c;也常被稱為電子標簽或無線射頻識別。它是一種非接觸式的自動識別技術&#xff0c;通過射頻信號自動識別目標對象并獲取相關數據&#xff0c;識別過程無需人工干預&…

[云原生] k8s之pod容器

一、pod的相關知識 1.1 Pod基礎概念 Pod是kubernetes中最小的資源管理組件&#xff0c;Pod也是最小化運行容器化應用的資源對象。一個Pod代表著集群中運行的一個進程。kubernetes中其他大多數組件都是圍繞著Pod來進行支撐和擴展Pod功能的&#xff0c;例如&#xff0c;用于管理…

如何確保JDK版本與操作系統架構匹配?

1. 序言 最近的工作中&#xff0c;需要升級JDK版本到17.0.7&#xff0c;以解決一個JDK bug&#xff1a;JDK-8299626該bug的core dump關鍵字如下&#xff1a;SIGSEGV in PhaseIdealLoop::build_loop_late_post_work公司JDK團隊提供的、包含JDK的基礎鏡像&#xff0c;有aarch64和…

Hololens2開發環境配置及項目生成部署

Hololens2開發環境配置及項目生成部署 Hololens2開發環境配置及項目生成部署一、官方文檔及推薦配置說明1.官方文檔介紹2.推薦配置及配置說明 二、安裝步驟0.現有Visual Stuido和Unity卸載1.Windows SDK安裝2.Visual Studio安裝3.Unity安裝4.MRTK配置 三、初次環境配置1.新建Un…

vmware的詭異網絡

坦白說&#xff0c;vmware的網絡我始終是沒有搞太懂。 我習慣使用的是“僅主機”和“NAT”。 “僅主機”我習慣配置靜態IP&#xff0c;互相訪問沒問題。 “NAT”則主要用于windows系統&#xff0c;簡單方便。windows虛擬機一般也沒啥特別的互相訪問需求&#xff0c;直接文件復制…

簡單了解GaussDB

大家都已經很熟悉openGauss了&#xff0c;昨天我的文章中說陜西電力的用采系統用Gaussdb替代了Oracle&#xff0c;就有朋友問我這個Gaussdb是不是就是openGauss。這個問題還真的有點不好回答&#xff0c;Gaussdb和openGauss淵源很近&#xff0c;但是還不是一碼事。華為在數據庫…

c++八股文:c++面向對象

文章目錄 1.c面向對象三大特性2.c的三個訪問修飾符3.多重繼承4.重載與重寫5.c多態怎么實現6.成員函數/成員變量/靜態成員函數/靜態成員變量的區別7.構造函數和析構函數8.c構造函數有幾種9.什么是虛函數什么是虛函數表10.虛函數和純虛函數的區別11.抽象類和純虛函數12.虛析構13.…

ABAP - SALV教程05 添加頁眉和頁腳

先看看效果叭CL_SALV_TABLE提供了SET_TOP_OF_LIST方法設置頁眉顯示和SET_TOP_OF_LIST_PRINT方法設置頁眉打印來實現添加頁眉的目的。CL_SALV_TABLE提供了SET_END_OF_LIST方法設置頁腳顯示和SET_END_OF_LIST_PRINT方法設置頁腳打印來實現添加頁腳的目的。這個四個方法的傳入參數…

Flutter異常上報及性能監控實現

1. 頁面異常監測 在Flutter中&#xff0c;通常用FlutterError監測Flutter框架拋出的異常&#xff0c;用runZonedGuarded監測應用中用戶代碼異常。 class AppGuarded {run(Widget app) {//1. 用FlutterError監測flutter框架拋出的異常FlutterError.onError (FlutterErrorDetail…

STM32F4 HAL庫串口死鎖問題調試記錄

文章目錄 STM32F4 HAL庫串口死鎖問題調試記錄調試方法結果分析解決方法一&#xff1a;方法二&#xff1a; STM32F4 HAL庫串口死鎖問題調試記錄 使用方法&#xff1a;通過串口DMA固定周期向外發送數據&#xff0c;同時開啟串口DMA接收用于接收其它板卡發來的數據。 問題&#x…

2024年,智慧文旅領航新時代,重塑旅行體驗的未來篇章!

隨著科技的飛速發展&#xff0c;智慧文旅正成為旅游行業的新寵&#xff0c;以其獨特的魅力和無限可能&#xff0c;引領著旅行體驗的全面升級。 2024年&#xff0c;智慧文旅火爆出圈&#xff0c;成為各界關注的焦點&#xff0c;為游客帶來了前所未有的震撼與享受。 智慧文旅&a…

290.【華為OD機試】連續出牌數量(深度優先搜索DFS—JavaPythonC++JS實現)

??點擊這里可直接跳轉到本專欄,可查閱頂置最新的華為OD機試寶典~ 本專欄所有題目均包含優質解題思路,高質量解題代碼(Java&Python&C++&JS分別實現),詳細代碼講解,助你深入學習,深度掌握! 文章目錄 一. 題目二.解題思路三.題解代碼Python題解代碼JAVA題解…

《猛獸派對》好玩嗎值得買嗎?蘋果電腦也能裝《猛獸派對》嗎?猛獸派對好友通行證 動物派對 猛獸對戰游戲

目錄 一、《猛獸派對》好玩嗎&#xff1f; 游戲玩法&#xff1a; 物理引擎&#xff1a; 關卡設計&#xff1a; 游戲特色&#xff1a; 評價&#xff1a; 榮譽&#xff1a; 二、蘋果電腦也能裝《猛獸派對》嗎&#xff1f; 第1步&#xff1a;下載并安裝CrossOver這款軟件…