表结构约定:
emp雇员表(empno员工号/ename员工姓名/job工作/mgr上级编号/hiredate受雇日期/sal薪金/comm佣金/deptno部门编号)
dept部门表(deptno部门编号/dname部门名称/loc地点)
(1) 掌握MySQL子查询的使用方法
1、列出至少有一个员工的所有部门。(两个表联合查询,及groupby...having的用法)
Select
2/ 列出所有员工的姓名及其直接上级的姓名。(多次对自己查询,为表的取个别名,内部查询可以像对象一样引用外部的对象的字段,这里引用与编程中的作用域相似,即与{}类比)
3、列出受雇日期早于其直接上级的所有员工。(同上,日期可直接拿来比较)
4、列出所有“CLERK”(办事员)的姓名及其部门名称。
5、列出最低薪金大于1500的各种工作
6、列出薪金高于公司平均薪金的所有员工。(反复查自己)
7、列出与“SCOTT”从事相同工作的所有员工。(排除自己)
8、列出薪金等于部门30中员工的薪金的所有员工的姓名和薪金。(any的用法,且排挤)
9、列出薪金高于在部门30工作的所有员工的薪金的员工姓名和薪金。(max的用法)
10、列出在每个(每个是关键字,对此group by)部门工作的员工数量、平均工资和平均服务期限。(经典的group by用法)
11、列出所有员工的姓名、部门名称和工资.(经典的两个表的连接查询,用具体的名称替换一个表中的主键的id (解决很多人在实际运用中会遇到的不能绑定多列的问题),也可用where来查询 ,与题5比较)
12、列出从事同一种工作但属于不同部门的员工的一种组合
13、列出所有部门的详细信息和部门人数。(因为是*,将显示dept和后面临时表b的全部字段(注意:不只是dept的字段,注意*号))
14、列出各种(与每个同义(参看题13))工作的最低工资
15、列出各个部门的MANAGER(经理,经理唯一,不用groupby)的最低薪金
Enter password: ***
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.13-enterprise-commercial-advanced MySQL Enterprise Server -
Advanced Edition (Commercial)
Copyright (c) 2000, 2013, 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| csdn |
| csdn3g |
| csdn9 |
| fenzu |
| gujianpeng |
| mysql |
| performance_schema |
| shujuku |
| student |
| waijian |
| webnews |
| yunsuanfu |
| zuoye |
+--------------------+
14 rows in set (1.03 sec)
mysql> create database yuebing;
Query OK, 1 row affected (0.07 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| csdn |
| csdn3g |
| csdn9 |
| fenzu |
| gujianpeng |
| mysql |
| performance_schema |
| shujuku |
| student |
| waijian |
| webnews |
| yuebing |
| yunsuanfu |
| zuoye |
+--------------------+
15 rows in set (0.00 sec)
mysql> use yuebing;
Database changed
创建一个数据库名叫 emp
mysql> create table emp(empno varchar(10),ename varchar(10),job varchar(10),mgr
varchar(10),hiredate varchar(10),sal varchar(10),comm varchar(10),deptno varchar
(10));
Query OK, 0 rows affected (1.73 sec)
mysql> desc emp;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| empno | varchar(10) | YES | | NULL | |
| ename | varchar(10) | YES | | NULL | |
| job | varchar(10) | YES | | NULL | |
| mgr | varchar(10) | YES | | NULL | |
| hiredate | varchar(10) | YES | | NULL | |
| sal | varchar(10) | YES | | NULL | |
| comm | varchar(10) | YES | | NULL | |
| deptno | varchar(10) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
8 rows in set (0.15 sec)
插入 emp的数据库信息
mysql> insert into emp values(7369,'Smith','Clerk',7902,'1980-12-27',800,0,20);
Query OK, 1 row affected (0.15 sec)
mysql> insert into emp values(7499,'Aleen','Salesman',7698,'1981-2-20',1600,300,
30);
Query OK, 1 row affected (0.10 sec)
mysql> insert into emp values(7844,'Turner','Jack',7499,'1981-9-8',1500,30,30);
Query OK, 1 row affected (0.06 sec)
mysql> insert into emp values(7698,'Tom','Manager',7986,'1981-9-8',6500,600,40);
Query OK, 1 row affected (0.14 sec)
mysql> insert into emp values(7876,'Adams','Clerk',7900,'1987-5-23',1100,0,20);
Query OK, 1 row affected (0.08 sec)
mysql> insert into emp values(7900,'James','Clerk',7698,'1981-12-23',1800,0,50);
Query OK, 1 row affected (0.07 sec)
mysql> insert into emp values(7903,'Ford','Analyst',7698,'1981-6-23',4800,20,30)
;
Query OK, 1 row affected (0.05 sec)
mysql> insert into emp values(7901,'Kily','Clerk',7900,'1921-2-4',1800,20,40);
Query OK, 1 row affected (0.09 sec)
查询插入的新数据
mysql> select * from emp;
+-------+--------+----------+------+------------+------+------+--------+
| empno | ename | job | mgr | hiredate | sal | comm | deptno |
+-------+--------+----------+------+------------+------+------+--------+
| 7369 | Smith | Clerk | 7902 | 1980-12-27 | 800 | 0 | 20 |
| 7499 | Aleen | Salesman | 7698 | 1981-2-20 | 1600 | 300 | 30 |
| 7844 | Turner | Jack | 7499 | 1981-9-8 | 1500 | 30 | 30 |
| 7698 | Tom | Manager | 7986 | 1981-9-8 | 6500 | 600 | 40 |
| 7876 | Adams | Clerk | 7900 | 1987-5-23 | 1100 | 0 | 20 |
| 7900 | James | Clerk | 7698 | 1981-12-23 | 1800 | 0 | 50 |
| 7903 | Ford | Analyst | 7698 | 1981-6-23 | 4800 | 20 | 30 |
| 7901 | Kily | Clerk | 7900 | 1921-2-4 | 1800 | 20 | 40 |
+-------+--------+----------+------+------------+------+------+--------+
8 rows in set (0.03 sec)
mysql>
插入的dept 数据库
mysql> create table dept(deptno varchar(10),dname varchar(10),loc varchar(10));
Query OK, 0 rows affected (0.66 sec)
mysql> insert into dept values(10,'Accounting','New York');
Query OK, 1 row affected (0.26 sec)
mysql> insert into dept values(20,'Research','Dallas');
Query OK, 1 row affected (0.09 sec)
mysql> insert into dept values(30,'Sales','Chicago');
Query OK, 1 row affected (0.05 sec)
mysql> insert into dept values(40,'Operations','Boston');
Query OK, 1 row affected (0.12 sec)
mysql> insert into dept values(50,'Admin','Washing');
Query OK, 1 row affected (0.14 sec)
查询的dept的新数据
mysql> select * from dept;
+--------+------------+----------+
| deptno | dname | loc |
+--------+------------+----------+
| 10 | Accounting | New York |
| 20 | Research | Dallas |
| 30 | Sales | Chicago |
| 40 | Operations | Boston |
| 50 | Admin | Washing |
+--------+------------+----------+
5 rows in set (0.00 sec)
mysql>
表结构约定:
emp雇员表(empno员工号/ename员工姓名/job工作/mgr上级编号/hiredate受雇日期/sal薪金/comm佣金/deptno部门编号)
dept部门表(deptno部门编号/dname部门名称/loc地点)
1、列出至少有一个员工的所有部门。(两个表联合查询,及group by...having的用法)
用法如下::::
分组有条件 而已啊 group by sex having count(*)>4
意思是:::按sex分组 并且有 数量 大于4 的条件
按sex分组 并且有 数量 大于 的条件
select count(1) from emp where id<100 group by sex having count(1)>4
having 其实和where一样都是过虑的作用,只是顺序不同,
在有分组的时候(group by )
where 是先过虑再分组计算
having是先分组计算再过虑.
1、列出至少有一个员工的所有部门。(两个表联合查询,及group by...having的用法)
mysql> select * from dept where deptno in(select deptno from emp group by deptno
having count(*)>1); ===>>至少有一个员工的所有部门。
+--------+------------+---------+
| deptno | dname | loc |
+--------+------------+---------+
| 20 | Research | Dallas |
| 30 | Sales | Chicago |
| 40 | Operations | Boston |
+--------+------------+---------+
3 rows in set (0.09 sec)
mysql>
2/ 列出所有员工的姓名及其直接上级的姓名。(多次对自己查询,为表的取个别名,
内部查询可以像对象一样引用外部的对象的字段,这里引用与编程中的作用域相似,即与{}类比)
mysql> select ename,(select ename from emp where empno=a.mgr)from emp a;
+--------+-------------------------------------------+
| ename | (select ename from emp where empno=a.mgr) |
+--------+-------------------------------------------+
| Smith | NULL |
| Aleen | Tom |
| Turner | Aleen |
| Tom | NULL |
| Adams | James |
| James | Tom |
| Ford | Tom |
| Kily | James |
+--------+-------------------------------------------+
8 rows in set (0.04 sec)
3 、列出受雇日期早于其直接上级的所有员工。 ( 同上 , 日期可直接拿来比较 )
mysql> select ename from emp a where hiredate<(select hiredate from emp where e
mpno=a.mgr);
+-------+
| ename |
+-------+
| Aleen |
| James |
| Ford |
| Kily |
+-------+
4 rows in set (0.00 sec)
mysql>
4 、列出所有“ CLERK ”(办事员)的姓名及其部门名称
mysql> select (select dname from dept where deptno=a.deptno) as dname,ename from
emp a where job='Clerk';
+------------+-------+
| dname | ename |
+------------+-------+
| Research | Smith |
| Research | Adams |
| Admin | James |
| Operations | Kily |
+------------+-------+
4 rows in set (0.00 sec)
mysql>
5 、列出最低薪金大于 1500 的各种工作
mysql> select job,min(sal) msal from emp group by job having min(sal)>1500;
+----------+------+
| job | msal |
+----------+------+
| Analyst | 4800 |
| Manager | 6500 |
| Salesman | 1600 |
+----------+------+
3 rows in set (0.03 sec)
mysql>
6 、列出薪金高于公司平均薪金的所有员工。 ( 反复查自己 )
mysql> select ename from emp where sal>(select avg(sal) from emp);
+-------+
| ename |
+-------+
| Tom |
| Ford |
+-------+
2 rows in set (0.04 sec)
mysql>
添加新的数据
mysql> insert into emp values(7586,'Scott','dasao',7269,'1983-2-24',50,23,80);
Query OK, 1 row affected (0.10 sec)
mysql> insert into emp values(7586,'sioas','dasao',7269,'1983-2-24',50,23,80);
Query OK, 1 row affected (0.07 sec)
7 、列出与“ SCOTT ”从事相同工作的所有员工。 ( 排除自己 )
mysql> select * from emp where job=(select job from emp where ename='SCOTT')and ename!='Scott';
+-------+-------+-------+------+-----------+------+------+--------+
| empno | ename | job | mgr | hiredate | sal | comm | deptno |
+-------+-------+-------+------+-----------+------+------+--------+
| 7586 | Scott | dasao | 7269 | 1983-2-24 | 50 | 23 | 80 |
| 7586 | sioas | dasao | 7269 | 1983-2-24 | 50 | 23 | 80 |
+-------+-------+-------+------+-----------+------+------+--------+
2 rows in set (0.00 sec)
8888888888888888888888888888
列出薪金等于部门 30 中员工的薪金的所有员工的姓名和薪金。 (any 的用法,且排挤 )
mysql> select ename,sal from emp where sal= any (select sal from emp where dept
o=30);
+--------+------+
| ename | sal |
+--------+------+
| Aleen | 1600 |
| Turner | 1500 |
| Ford | 4800 |
+--------+------+
3 rows in set (0.94 sec)
mysql> select ename,sal from emp where sal= any (select sal from emp where dept
o!=30);
+-------+------+
| ename | sal |
+-------+------+
| Smith | 800 |
| Tom | 6500 |
| Adams | 1100 |
| James | 1800 |
| Kily | 1800 |
| Scott | 50 |
| sioas | 50 |
+-------+------+
7 rows in set (0.00 sec)
9、列出薪金高于在部门30工作的所有员工的薪金的员工姓名和薪金。(max的用法)
mysql> select sal,ename from emp where sal>(select max(sal) from emp where deptn
o=30);
+------+-------+
| sal | ename |
+------+-------+
| 800 | Smith |
| 6500 | Tom |
| 50 | Scott |
| 50 | sioas |
+------+-------+
4 rows in set (0.06 sec)
mysql>
10、列出在每个(每个是关键字,对此group by)部门工作的员工数量、平均工资和平均服务期限。(经典的group by用法)
mysql> select count(ename), avg(sal),avg(now()-hiredate) from emp group by deptn
o;
+--------------+--------------------+---------------------+
| count(ename) | avg(sal) | avg(now()-hiredate) |
+--------------+--------------------+---------------------+
| 2 | 950 | 20130923191733.5 |
| 3 | 2633.3333333333335 | 20130923191736 |
| 2 | 4150 | 20130923191766 |
| 1 | 1800 | 20130923191736 |
| 2 | 50 | 20130923191734 |
+--------------+--------------------+---------------------+
5 rows in set, 10 warnings (0.20 sec)
11、列出所有员工的姓名、部门名称和工资.(经典的两个表的连接查询,用具体的名称替换一个表中的主键的id (解决很多人在实际运用中会遇到的不能绑定多列的问题),也可用where来查询 ,与题5比较)
mysql> select e.ename,d.dname,e.sal from emp e, dept d where e.deptno=d.deptno;
+--------+------------+------+
| ename | dname | sal |
+--------+------------+------+
| Smith | Research | 800 |
| Aleen | Sales | 1600 |
| Turner | Sales | 1500 |
| Tom | Operations | 6500 |
| Adams | Research | 1100 |
| James | Admin | 1800 |
| Ford | Sales | 4800 |
| Kily | Operations | 1800 |
+--------+------------+------+
8 rows in set (0.02 sec)
12、列出从事同一种工作但属于不同部门的员工的一种组合
mysql> select e1.ename,e1.job,e1.deptno,e2.ename,e2.job,e2.deptno from emp e1,em
p e2 where e1.job=e2.job and e1.deptno!=e2.deptno;
+-------+-------+--------+-------+-------+--------+
| ename | job | deptno | ename | job | deptno |
+-------+-------+--------+-------+-------+--------+
| James | Clerk | 50 | Smith | Clerk | 20 |
| Kily | Clerk | 40 | Smith | Clerk | 20 |
| James | Clerk | 50 | Adams | Clerk | 20 |
| Kily | Clerk | 40 | Adams | Clerk | 20 |
| Smith | Clerk | 20 | James | Clerk | 50 |
| Adams | Clerk | 20 | James | Clerk | 50 |
| Kily | Clerk | 40 | James | Clerk | 50 |
| Smith | Clerk | 20 | Kily | Clerk | 40 |
| Adams | Clerk | 20 | Kily | Clerk | 40 |
| James | Clerk | 50 | Kily | Clerk | 40 |
+-------+-------+--------+-------+-------+--------+
10 rows in set (0.07 sec)
13、列出所有部门的详细信息和部门人数。(因为是*,将显示dept和后面临时表b的全部字段(注意:不只是dept的字段,注意*号))
mysql> select * from dept a left join (select deptno,count(*) from emp group by
deptno) b on a.deptno=b.deptno ;
+--------+------------+----------+--------+----------+
| deptno | dname | loc | deptno | count(*) |
+--------+------------+----------+--------+----------+
| 10 | Accounting | New York | NULL | NULL |
| 20 | Research | Dallas | 20 | 2 |
| 30 | Sales | Chicago | 30 | 3 |
| 40 | Operations | Boston | 40 | 2 |
| 50 | Admin | Washing | 50 | 1 |
+--------+------------+----------+--------+----------+
5 rows in set (0.09 sec)
14、列出各种(与每个同义(参看题13))工作的最低工资
mysql> select job,min(sal) from emp group by job;
+----------+----------+
| job | min(sal) |
+----------+----------+
| Analyst | 4800 |
| Clerk | 1100 |
| dasao | 50 |
| Jack | 1500 |
| Manager | 6500 |
| Salesman | 1600 |
+----------+----------+
6 rows in set (0.02 sec)
15、列出各个部门的MANAGER(经理,经理唯一,不用group by)的最低薪金
mysql> select min(sal) from emp where job like 'MANAGER';
+----------+
| min(sal) |
+----------+
| 6500 |
+----------+
1 row in set (0.05 sec)