MySQL的单表和多表查询
我们在前面曾构建过三个用于实验的表格,下面将基于这三个表进行实践。
# 建立一个用于实验的三个表格
mysql> create table emp (-> empno varchar(10),-> ename varchar(50),-> job varchar(50),-> mgr int,-> hiredate timestamp,-> sal decimal(10, 2),-> comm decimal(10, 2),-> deptno int-> );
Query OK, 0 rows affected (0.03 sec)mysql> insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno) values-> ('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);
Query OK, 14 rows affected (0.01 sec)
Records: 14 Duplicates: 0 Warnings: 0mysql> create table dept (-> deptno int,-> dname varchar(50),-> loc varchar(50)-> );
Query OK, 0 rows affected (0.03 sec)mysql> insert into dept (deptno, dname, loc) values-> (10, 'accounting', 'new york'),-> (20, 'research', 'dallas'),-> (30, 'sales', 'chicago'),-> (40, 'operations', 'boston');
Query OK, 4 rows affected (0.01 sec)
Records: 4 Duplicates: 0 Warnings: 0mysql> create table salgrade (-> grade int,-> losal int,-> hisal int-> );
Query OK, 0 rows affected (0.03 sec)mysql> insert into salgrade (grade, losal, hisal) values-> (1, 700, 1200),-> (2, 1201, 1400),-> (3, 1401, 2000),-> (4, 2001, 3000),-> (5, 3001, 9999);
Query OK, 5 rows affected (0.01 sec)
Records: 5 Duplicates: 0 Warnings: 0mysql> 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 * 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> 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> alter table dept modify column deptno int not null, add primary key (deptno);
Query OK, 0 rows affected (0.07 sec)
Records: 0 Duplicates: 0 Warnings: 0# 添加外键
mysql> alter table emp add foreign key (deptno) references dept(deptno);
Query OK, 14 rows affected (0.08 sec)
Records: 14 Duplicates: 0 Warnings: 0# 查看设置
mysql> desc emp;
+----------+---------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------------+------+-----+-------------------+-----------------------------+
| empno | varchar(10) | YES | | NULL | |
| ename | varchar(50) | YES | | NULL | |
| job | varchar(50) | YES | | NULL | |
| mgr | int(11) | YES | | NULL | |
| hiredate | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| sal | decimal(10,2) | YES | | NULL | |
| comm | decimal(10,2) | YES | | NULL | |
| deptno | int(11) | YES | MUL | NULL | |
+----------+---------------+------+-----+-------------------+-----------------------------+
8 rows in set (0.00 sec)mysql> desc dept;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| deptno | int(11) | NO | PRI | NULL | |
| dname | varchar(50) | YES | | NULL | |
| loc | varchar(50) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
1.单表查询
-
查询工资高于
1000
或岗位为manager
的雇员,同时还要满足他们的姓名首字母为j
# 需求 1 mysql> select * from emp where ((sal>1000 or job='manager') and left(ename, 1)='j'); +--------+-------+---------+------+---------------------+---------+------+--------+ | empno | ename | job | mgr | hiredate | sal | comm | deptno | +--------+-------+---------+------+---------------------+---------+------+--------+ | 007566 | jones | manager | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL | 20 | +--------+-------+---------+------+---------------------+---------+------+--------+ 1 row in set (0.00 sec)select * from EMP where (sal>500 or job='MANAGER') and ename like 'J%'; mysql> select * from emp where (sal>1000 or job='manager') and ename like 'J%';; +--------+-------+---------+------+---------------------+---------+------+--------+ | empno | ename | job | mgr | hiredate | sal | comm | deptno | +--------+-------+---------+------+---------------------+---------+------+--------+ | 007566 | jones | manager | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL | 20 | +--------+-------+---------+------+---------------------+---------+------+--------+ 1 row in set (0.00 sec)
-
按照部门号升序、雇员的工资降序来排序
# 需求 2 mysql> select sal, deptno, ename from emp order by deptno asc, sal desc; +---------+--------+--------+ | sal | deptno | ename | +---------+--------+--------+ | 5000.00 | 10 | king | | 2450.00 | 10 | clark | | 1300.00 | 10 | miller | | 3000.00 | 20 | scott | | 3000.00 | 20 | ford | | 2975.00 | 20 | jones | | 1100.00 | 20 | adams | | 800.00 | 20 | smith | | 2850.00 | 30 | blake | | 1600.00 | 30 | allen | | 1500.00 | 30 | turner | | 1250.00 | 30 | ward | | 1250.00 | 30 | martin | | 950.00 | 30 | james | +---------+--------+--------+ 14 rows in set (0.00 sec)
-
使用年薪(年薪=月薪*12+奖金)进行降序排序
# 需求 3 mysql> select ename, sal*12+ifnull(comm,0) as 年薪 from emp order by '年薪' desc; +--------+----------+ | ename | 年薪 | +--------+----------+ | smith | 9600.00 | | allen | 19500.00 | | ward | 15500.00 | | jones | 35700.00 | | martin | 16400.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 | +--------+----------+ 14 rows in set (0.00 sec)
-
显示工资最高的员工的名字和工作岗位
# 需求 4 mysql> select max(sal) from emp; +----------+ | max(sal) | +----------+ | 5000.00 | +----------+ 1 row in set (0.00 sec)mysql> select ename, job from emp where sal=5000; +-------+-----------+ | ename | job | +-------+-----------+ | king | president | +-------+-----------+ 1 row in set (0.00 sec)mysql> select ename, job from emp where sal=(select max(sal) from emp); # 复合查找,也叫“查找子句” +-------+-----------+ | ename | job | +-------+-----------+ | king | president | +-------+-----------+ 1 row in set (0.01 sec)
-
显示工资高于平均工资的员工信息
# 需求 5 mysql> select * from emp where sal > (select avg(sal) from emp); +--------+-------+-----------+------+---------------------+---------+------+--------+ | empno | ename | job | mgr | hiredate | sal | comm | deptno | +--------+-------+-----------+------+---------------------+---------+------+--------+ | 007566 | jones | manager | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL | 20 | | 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 | | 007902 | ford | analyst | 7566 | 1981-12-03 00:00:00 | 3000.00 | NULL | 20 | +--------+-------+-----------+------+---------------------+---------+------+--------+ 6 rows in set (0.00 sec)
-
显示每个部门的平均工资和最高工资
# 需求 6 mysql> select deptno, avg(sal), max(sal) from emp group by deptno; +--------+-------------+----------+ | deptno | avg(sal) | max(sal) | +--------+-------------+----------+ | 10 | 2916.666667 | 5000.00 | | 20 | 2175.000000 | 3000.00 | | 30 | 1566.666667 | 2850.00 | +--------+-------------+----------+ 3 rows in set (0.00 sec)
-
显示平均工资低于
2000
的部门号和它的平均工资# 需求 7 mysql> select deptno, avg(sal) 平均工资 from emp group by deptno having 平均工资<2000; +--------+--------------+ | deptno | 平均工资 | +--------+--------------+ | 30 | 1566.666667 | +--------+--------------+ 1 row in set (0.01 sec)
-
显示每种岗位的雇员总数,平均工资
# 需求 8 mysql> select job, format(avg(sal), 2) 平均工资, count(*) 人数 from emp group by job; +-----------+--------------+--------+ | job | 平均工资 | 人数 | +-----------+--------------+--------+ | analyst | 3,000.00 | 2 | | clerk | 1,037.50 | 4 | | manager | 2,758.33 | 3 | | president | 5,000.00 | 1 | | salesman | 1,400.00 | 4 | +-----------+--------------+--------+ 5 rows in set (0.00 sec)
2.多表查询
-
显示雇员名、雇员工资以及所在部门的名字
# 需求 1 # 将表合外表整合为一个表 mysql> select * from emp, dept where emp.deptno=dept.deptno; +--------+--------+-----------+------+---------------------+---------+---------+--------+--------+------------+----------+ | empno | ename | job | mgr | hiredate | sal | comm | deptno | deptno | dname | loc | +--------+--------+-----------+------+---------------------+---------+---------+--------+--------+------------+----------+ | 007782 | clark | manager | 7839 | 1981-06-09 00:00:00 | 2450.00 | NULL | 10 | 10 | accounting | new york | | 007839 | king | president | NULL | 1981-11-17 00:00:00 | 5000.00 | NULL | 10 | 10 | accounting | new york | | 007934 | miller | clerk | 7782 | 1982-01-23 00:00:00 | 1300.00 | NULL | 10 | 10 | accounting | new york | | 007369 | smith | clerk | 7902 | 1980-12-17 00:00:00 | 800.00 | NULL | 20 | 20 | research | dallas | | 007566 | jones | manager | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL | 20 | 20 | research | dallas | | 007788 | scott | analyst | 7566 | 1987-04-19 00:00:00 | 3000.00 | NULL | 20 | 20 | research | dallas | | 007876 | adams | clerk | 7788 | 1987-05-23 00:00:00 | 1100.00 | NULL | 20 | 20 | research | dallas | | 007902 | ford | analyst | 7566 | 1981-12-03 00:00:00 | 3000.00 | NULL | 20 | 20 | research | dallas | | 007499 | allen | salesman | 7698 | 1981-02-20 00:00:00 | 1600.00 | 300.00 | 30 | 30 | sales | chicago | | 007521 | ward | salesman | 7698 | 1981-02-22 00:00:00 | 1250.00 | 500.00 | 30 | 30 | sales | chicago | | 007654 | martin | salesman | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 | 30 | 30 | sales | chicago | | 007698 | blake | manager | 7839 | 1981-05-01 00:00:00 | 2850.00 | NULL | 30 | 30 | sales | chicago | | 007844 | turner | salesman | 7698 | 1981-09-08 00:00:00 | 1500.00 | 0.00 | 30 | 30 | sales | chicago | | 007900 | james | clerk | 7698 | 1981-12-03 00:00:00 | 950.00 | NULL | 30 | 30 | sales | chicago | +--------+--------+-----------+------+---------------------+---------+---------+--------+--------+------------+----------+ 14 rows in set (0.00 sec)mysql> select emp.ename sal dname from emp, dept where emp.deptno=dept.deptno; 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 'dname from emp, dept where emp.deptno=dept.deptno' at line 1 mysql> select emp.ename sal, dname, from emp, dept where emp.deptno=dept.deptno; 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 'from emp, dept where emp.deptno=dept.deptno' at line 1 mysql> select emp.ename, sal, dname from emp, dept where emp.deptno=dept.deptno; +--------+---------+------------+ | ename | sal | dname | +--------+---------+------------+ | clark | 2450.00 | accounting | | king | 5000.00 | accounting | | miller | 1300.00 | accounting | | smith | 800.00 | research | | jones | 2975.00 | research | | scott | 3000.00 | research | | adams | 1100.00 | research | | ford | 3000.00 | research | | allen | 1600.00 | sales | | ward | 1250.00 | sales | | martin | 1250.00 | sales | | blake | 2850.00 | sales | | turner | 1500.00 | sales | | james | 950.00 | sales | +--------+---------+------------+ 14 rows in set (0.00 sec)
-
显示部门号为
10
的部门名,员工名和工资# 需求 2 mysql> select dept.dname ,emp.ename, emp.sal from emp, dept where emp.deptno=dept.deptno and emp.deptno=10; +------------+--------+---------+ | dname | ename | sal | +------------+--------+---------+ | accounting | clark | 2450.00 | | accounting | king | 5000.00 | | accounting | miller | 1300.00 | +------------+--------+---------+ 3 rows in set (0.00 sec)
-
显示各个员工的姓名,工资,及工资级别
# 需求 3 # (1)查看工资等级 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)# (2)查看员工信息表 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)# (3)整合两表(求笛卡尔积) mysql> select * from emp, salgrade; +--------+--------+-----------+------+---------------------+---------+---------+--------+-------+-------+-------+ | empno | ename | job | mgr | hiredate | sal | comm | deptno | grade | losal | hisal | +--------+--------+-----------+------+---------------------+---------+---------+--------+-------+-------+-------+ | 007369 | smith | clerk | 7902 | 1980-12-17 00:00:00 | 800.00 | NULL | 20 | 1 | 700 | 1200 | | 007369 | smith | clerk | 7902 | 1980-12-17 00:00:00 | 800.00 | NULL | 20 | 2 | 1201 | 1400 | | 007369 | smith | clerk | 7902 | 1980-12-17 00:00:00 | 800.00 | NULL | 20 | 3 | 1401 | 2000 | | 007369 | smith | clerk | 7902 | 1980-12-17 00:00:00 | 800.00 | NULL | 20 | 4 | 2001 | 3000 | | 007369 | smith | clerk | 7902 | 1980-12-17 00:00:00 | 800.00 | NULL | 20 | 5 | 3001 | 9999 | | 007499 | allen | salesman | 7698 | 1981-02-20 00:00:00 | 1600.00 | 300.00 | 30 | 1 | 700 | 1200 | | 007499 | allen | salesman | 7698 | 1981-02-20 00:00:00 | 1600.00 | 300.00 | 30 | 2 | 1201 | 1400 | | 007499 | allen | salesman | 7698 | 1981-02-20 00:00:00 | 1600.00 | 300.00 | 30 | 3 | 1401 | 2000 | | 007499 | allen | salesman | 7698 | 1981-02-20 00:00:00 | 1600.00 | 300.00 | 30 | 4 | 2001 | 3000 | | 007499 | allen | salesman | 7698 | 1981-02-20 00:00:00 | 1600.00 | 300.00 | 30 | 5 | 3001 | 9999 | | 007521 | ward | salesman | 7698 | 1981-02-22 00:00:00 | 1250.00 | 500.00 | 30 | 1 | 700 | 1200 | | 007521 | ward | salesman | 7698 | 1981-02-22 00:00:00 | 1250.00 | 500.00 | 30 | 2 | 1201 | 1400 | | 007521 | ward | salesman | 7698 | 1981-02-22 00:00:00 | 1250.00 | 500.00 | 30 | 3 | 1401 | 2000 | | 007521 | ward | salesman | 7698 | 1981-02-22 00:00:00 | 1250.00 | 500.00 | 30 | 4 | 2001 | 3000 | | 007521 | ward | salesman | 7698 | 1981-02-22 00:00:00 | 1250.00 | 500.00 | 30 | 5 | 3001 | 9999 | | 007566 | jones | manager | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL | 20 | 1 | 700 | 1200 | | 007566 | jones | manager | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL | 20 | 2 | 1201 | 1400 | | 007566 | jones | manager | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL | 20 | 3 | 1401 | 2000 | | 007566 | jones | manager | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL | 20 | 4 | 2001 | 3000 | | 007566 | jones | manager | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL | 20 | 5 | 3001 | 9999 | | 007654 | martin | salesman | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 | 30 | 1 | 700 | 1200 | | 007654 | martin | salesman | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 | 30 | 2 | 1201 | 1400 | | 007654 | martin | salesman | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 | 30 | 3 | 1401 | 2000 | | 007654 | martin | salesman | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 | 30 | 4 | 2001 | 3000 | | 007654 | martin | salesman | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 | 30 | 5 | 3001 | 9999 | | 007698 | blake | manager | 7839 | 1981-05-01 00:00:00 | 2850.00 | NULL | 30 | 1 | 700 | 1200 | | 007698 | blake | manager | 7839 | 1981-05-01 00:00:00 | 2850.00 | NULL | 30 | 2 | 1201 | 1400 | | 007698 | blake | manager | 7839 | 1981-05-01 00:00:00 | 2850.00 | NULL | 30 | 3 | 1401 | 2000 | | 007698 | blake | manager | 7839 | 1981-05-01 00:00:00 | 2850.00 | NULL | 30 | 4 | 2001 | 3000 | | 007698 | blake | manager | 7839 | 1981-05-01 00:00:00 | 2850.00 | NULL | 30 | 5 | 3001 | 9999 | | 007782 | clark | manager | 7839 | 1981-06-09 00:00:00 | 2450.00 | NULL | 10 | 1 | 700 | 1200 | | 007782 | clark | manager | 7839 | 1981-06-09 00:00:00 | 2450.00 | NULL | 10 | 2 | 1201 | 1400 | | 007782 | clark | manager | 7839 | 1981-06-09 00:00:00 | 2450.00 | NULL | 10 | 3 | 1401 | 2000 | | 007782 | clark | manager | 7839 | 1981-06-09 00:00:00 | 2450.00 | NULL | 10 | 4 | 2001 | 3000 | | 007782 | clark | manager | 7839 | 1981-06-09 00:00:00 | 2450.00 | NULL | 10 | 5 | 3001 | 9999 | | 007788 | scott | analyst | 7566 | 1987-04-19 00:00:00 | 3000.00 | NULL | 20 | 1 | 700 | 1200 | | 007788 | scott | analyst | 7566 | 1987-04-19 00:00:00 | 3000.00 | NULL | 20 | 2 | 1201 | 1400 | | 007788 | scott | analyst | 7566 | 1987-04-19 00:00:00 | 3000.00 | NULL | 20 | 3 | 1401 | 2000 | | 007788 | scott | analyst | 7566 | 1987-04-19 00:00:00 | 3000.00 | NULL | 20 | 4 | 2001 | 3000 | | 007788 | scott | analyst | 7566 | 1987-04-19 00:00:00 | 3000.00 | NULL | 20 | 5 | 3001 | 9999 | | 007839 | king | president | NULL | 1981-11-17 00:00:00 | 5000.00 | NULL | 10 | 1 | 700 | 1200 | | 007839 | king | president | NULL | 1981-11-17 00:00:00 | 5000.00 | NULL | 10 | 2 | 1201 | 1400 | | 007839 | king | president | NULL | 1981-11-17 00:00:00 | 5000.00 | NULL | 10 | 3 | 1401 | 2000 | | 007839 | king | president | NULL | 1981-11-17 00:00:00 | 5000.00 | NULL | 10 | 4 | 2001 | 3000 | | 007839 | king | president | NULL | 1981-11-17 00:00:00 | 5000.00 | NULL | 10 | 5 | 3001 | 9999 | | 007844 | turner | salesman | 7698 | 1981-09-08 00:00:00 | 1500.00 | 0.00 | 30 | 1 | 700 | 1200 | | 007844 | turner | salesman | 7698 | 1981-09-08 00:00:00 | 1500.00 | 0.00 | 30 | 2 | 1201 | 1400 | | 007844 | turner | salesman | 7698 | 1981-09-08 00:00:00 | 1500.00 | 0.00 | 30 | 3 | 1401 | 2000 | | 007844 | turner | salesman | 7698 | 1981-09-08 00:00:00 | 1500.00 | 0.00 | 30 | 4 | 2001 | 3000 | | 007844 | turner | salesman | 7698 | 1981-09-08 00:00:00 | 1500.00 | 0.00 | 30 | 5 | 3001 | 9999 | | 007876 | adams | clerk | 7788 | 1987-05-23 00:00:00 | 1100.00 | NULL | 20 | 1 | 700 | 1200 | | 007876 | adams | clerk | 7788 | 1987-05-23 00:00:00 | 1100.00 | NULL | 20 | 2 | 1201 | 1400 | | 007876 | adams | clerk | 7788 | 1987-05-23 00:00:00 | 1100.00 | NULL | 20 | 3 | 1401 | 2000 | | 007876 | adams | clerk | 7788 | 1987-05-23 00:00:00 | 1100.00 | NULL | 20 | 4 | 2001 | 3000 | | 007876 | adams | clerk | 7788 | 1987-05-23 00:00:00 | 1100.00 | NULL | 20 | 5 | 3001 | 9999 | | 007900 | james | clerk | 7698 | 1981-12-03 00:00:00 | 950.00 | NULL | 30 | 1 | 700 | 1200 | | 007900 | james | clerk | 7698 | 1981-12-03 00:00:00 | 950.00 | NULL | 30 | 2 | 1201 | 1400 | | 007900 | james | clerk | 7698 | 1981-12-03 00:00:00 | 950.00 | NULL | 30 | 3 | 1401 | 2000 | | 007900 | james | clerk | 7698 | 1981-12-03 00:00:00 | 950.00 | NULL | 30 | 4 | 2001 | 3000 | | 007900 | james | clerk | 7698 | 1981-12-03 00:00:00 | 950.00 | NULL | 30 | 5 | 3001 | 9999 | | 007902 | ford | analyst | 7566 | 1981-12-03 00:00:00 | 3000.00 | NULL | 20 | 1 | 700 | 1200 | | 007902 | ford | analyst | 7566 | 1981-12-03 00:00:00 | 3000.00 | NULL | 20 | 2 | 1201 | 1400 | | 007902 | ford | analyst | 7566 | 1981-12-03 00:00:00 | 3000.00 | NULL | 20 | 3 | 1401 | 2000 | | 007902 | ford | analyst | 7566 | 1981-12-03 00:00:00 | 3000.00 | NULL | 20 | 4 | 2001 | 3000 | | 007902 | ford | analyst | 7566 | 1981-12-03 00:00:00 | 3000.00 | NULL | 20 | 5 | 3001 | 9999 | | 007934 | miller | clerk | 7782 | 1982-01-23 00:00:00 | 1300.00 | NULL | 10 | 1 | 700 | 1200 | | 007934 | miller | clerk | 7782 | 1982-01-23 00:00:00 | 1300.00 | NULL | 10 | 2 | 1201 | 1400 | | 007934 | miller | clerk | 7782 | 1982-01-23 00:00:00 | 1300.00 | NULL | 10 | 3 | 1401 | 2000 | | 007934 | miller | clerk | 7782 | 1982-01-23 00:00:00 | 1300.00 | NULL | 10 | 4 | 2001 | 3000 | | 007934 | miller | clerk | 7782 | 1982-01-23 00:00:00 | 1300.00 | NULL | 10 | 5 | 3001 | 9999 | +--------+--------+-----------+------+---------------------+---------+---------+--------+-------+-------+-------+ 70 rows in set (0.00 sec)# (4)列出员工姓名、员工工资、工资对应等级 mysql> select emp.ename, emp.sal, salgrade.grade from emp, salgrade where sal between salgrade.losal and salgrade.hisal; +--------+---------+-------+ | ename | sal | grade | +--------+---------+-------+ | smith | 800.00 | 1 | | allen | 1600.00 | 3 | | ward | 1250.00 | 2 | | jones | 2975.00 | 4 | | martin | 1250.00 | 2 | | blake | 2850.00 | 4 | | clark | 2450.00 | 4 | | scott | 3000.00 | 4 | | king | 5000.00 | 5 | | turner | 1500.00 | 3 | | adams | 1100.00 | 1 | | james | 950.00 | 1 | | ford | 3000.00 | 4 | | miller | 1300.00 | 2 | +--------+---------+-------+ 14 rows in set (0.00 sec)
补充:同一张表也可以进行笛卡尔积,也就是“自连接”
# 尝试自连接 mysql> select * from salgrade as t1, salgrade as t2; +-------+-------+-------+-------+-------+-------+ | grade | losal | hisal | grade | losal | hisal | +-------+-------+-------+-------+-------+-------+ | 1 | 700 | 1200 | 1 | 700 | 1200 | | 2 | 1201 | 1400 | 1 | 700 | 1200 | | 3 | 1401 | 2000 | 1 | 700 | 1200 | | 4 | 2001 | 3000 | 1 | 700 | 1200 | | 5 | 3001 | 9999 | 1 | 700 | 1200 | | 1 | 700 | 1200 | 2 | 1201 | 1400 | | 2 | 1201 | 1400 | 2 | 1201 | 1400 | | 3 | 1401 | 2000 | 2 | 1201 | 1400 | | 4 | 2001 | 3000 | 2 | 1201 | 1400 | | 5 | 3001 | 9999 | 2 | 1201 | 1400 | | 1 | 700 | 1200 | 3 | 1401 | 2000 | | 2 | 1201 | 1400 | 3 | 1401 | 2000 | | 3 | 1401 | 2000 | 3 | 1401 | 2000 | | 4 | 2001 | 3000 | 3 | 1401 | 2000 | | 5 | 3001 | 9999 | 3 | 1401 | 2000 | | 1 | 700 | 1200 | 4 | 2001 | 3000 | | 2 | 1201 | 1400 | 4 | 2001 | 3000 | | 3 | 1401 | 2000 | 4 | 2001 | 3000 | | 4 | 2001 | 3000 | 4 | 2001 | 3000 | | 5 | 3001 | 9999 | 4 | 2001 | 3000 | | 1 | 700 | 1200 | 5 | 3001 | 9999 | | 2 | 1201 | 1400 | 5 | 3001 | 9999 | | 3 | 1401 | 2000 | 5 | 3001 | 9999 | | 4 | 2001 | 3000 | 5 | 3001 | 9999 | | 5 | 3001 | 9999 | 5 | 3001 | 9999 | +-------+-------+-------+-------+-------+-------+ 25 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)
有些情况下是需要自连接的,例如“显示员工
ford
的上级领导的编号和姓名”:# 尝试寻找上级领导 # (1)查看员工表 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)# (2)查看某个员工的上级领导编号 mysql> select mgr from emp where ename='ford'; +------+ | mgr | +------+ | 7566 | +------+ 1 row in set (0.00 sec)# (3)查看领导编号对应的领导姓名 mysql> select ename, empno from emp where empno=7566; +-------+--------+ | ename | empno | +-------+--------+ | jones | 007566 | +-------+--------+ 1 row in set (0.00 sec)# (4)子查询做法 mysql> select ename, empno from emp where empno=(select mgr from emp where ename='ford'); +-------+--------+ | ename | empno | +-------+--------+ | jones | 007566 | +-------+--------+ 1 row in set (0.01 sec)# (5)自连接做法 mysql> select e2.empno 领导编号, e2.ename 领导名 from emp as e1, emp as e2 where e1.ename='ford' and e1.mgr=e2.empno; +--------------+-----------+ | 领导编号 | 领导名 | +--------------+-----------+ | 007566 | jones | +--------------+-----------+ 1 row in set (0.00 sec)
相关文章:
MySQL的单表和多表查询
我们在前面曾构建过三个用于实验的表格,下面将基于这三个表进行实践。 # 建立一个用于实验的三个表格 mysql> create table emp (-> empno varchar(10),-> ename varchar(50),-> job varchar(50),-> mgr int,-> hiredate timestamp,-&…...

攻防世界例题wp
1.看到_wakeup()函数第一反应要么触发,要么绕过在这里绕过 2.构造payload实例化一个对象后反序列化 3构造脚本如下: 4.因为它是一个绕过的方法所以我们要使用绕过的方法。 5.继续构造payload将上图的1换成2进行绕过 最终的payload为 O:4:"xctf…...

仿牛客网项目---显示评论和添加评论功能的实现
这篇文章,我来介绍一下我的项目中的另外一个功能:显示评论和添加评论。 其实这两个功能都不怎么重要,我感觉最重要的应该是用户注册登录功能,这个也了解一下,知道这么一回事儿就好。 首先设计DAO层。 Mapper public …...

idea集成git详解教程(实用篇)
0.Git常用命令 Git常用命令-CSDN博客 1.下载git Git - Downloads 一路傻瓜式安装即可(NEXT) 2.软件测试 在Windows桌面空白处,点击鼠标右键,弹出右键菜单 Git软件安装后,会在右键菜单中增加两个菜单 Git GUI He…...

搭建LNMP环境并配置个人博客系统
LNMP是Linux(操作系统)、Nginx(Web服务器)、MySQL(数据库)和PHP(脚本解释器)的组合,常用于部署高性能的动态网站,如WordPress等博客平台 一、安装Linux操作系…...

为什么网络安全缺口这么大,还是这么缺网络安全工程师?
为啥网安领域缺口多达300多万人,但网安工程师(白帽黑客)却很少,难道又是砖家在忽悠人??? 主要原因为这三点: 首先是学校的原因,很多学校网络安全课程用的还都是十年前的老教材&…...
创新洞察|诺奖获得者埃德蒙·费尔普斯谈未来:中国创新从何而来?
当下,全球宏观经济形势正在发生深刻变化,以创新推进高质量发展被推向前所未有的高度。作为多年与中国经济结伴而行的经济学家、2006年诺贝尔经济学奖得主,埃德蒙费尔普斯认为:创新无处不在,而中国人具有巨大的创新活力…...

SpringMVC 学习(六)之视图
目录 1 SpringMVC 视图介绍 2 JSP 视图 3 Thymeleaf 视图 4 FreeMarker 视图 5 XSLT 视图 6 请求转发与重定向 6.1 请求转发 (Forward) 6.2 重定向 (Redirect) 7 视图控制器 (view-controller) 1 SpringMVC 视图介绍 在 SpringMVC 框架中,视图可以是一个 J…...

ROS 2基础概念#1:计算图(Compute Graph)| ROS 2学习笔记
在ROS中,计算图(ROS Compute Graph)是一个核心概念,它描述了ROS节点之间的数据流动和通信方式。它不仅仅是一个通信网络,它也反映了ROS设计哲学的核心——灵活性、模块化和可重用性。通过细致探讨计算图的高级特性和实…...
一本通 1403:素数对
在判断素数对的两个数是否都为素数时可以只判断数的一半 #include<bits/stdc.h> using namespace std; bool su(int a,int b){ for(int i2;i<sqrt(a);i){ if(a%i0){ return 0; } } for(int i2;i<sqrt(b);i){ if(…...

华为---RSTP(四)---RSTP的保护功能简介和示例配置
目录 1. 技术背景 2. RSTP的保护功能 3. BPDU保护机制原理和配置命令 3.1 BPDU保护机制原理 3.2 BPDU保护机制配置命令 3.3 BPDU保护机制配置步骤 4. 根保护机制原理和配置命令 4.1 根保护机制原理 4.2 根保护机制配置命令 4.3 根保护机制配置步骤 5. 环路保护机…...
Android基础控件介绍
在Android应用程序开发中,使用基础控件是非常常见的。这些控件允许您在用户界面中显示文本、图像、按钮等元素,以及接收用户输入。本文将介绍几种常见的基础控件,并给出每个控件在示例XML中使用的属性的详细说明。 1. TextView TextView 是…...

【总结】Maxwell学习笔记
1.Maxwell简介 Maxwell 是一款用Java编写的MySQL变更数据抓取软件,它会实时监控Mysql数据库的数据变更操作(包括insert、update、delete),并将变更数据以 JSON 格式发送给 Kafka、Kinesi等流数据处理平台 官网地址:M…...
AFL fork server和fuzzer的交互
看了一些博客,都是在说fuzzer和fork server进行交互,由fork server fork出子进程来执行程序,但是不太明白这两者到底是如何在代码层面进行交互的。 run_target中有这么一段代码,大概意思是fuzzer给fork server传递prev_timed_out…...

Java SE:多线程(Thread)
1. 线程两个基本概念 并发:即线程交替运行多个指令并行:即多个线程同时运行指令 并发并行不矛盾,两者可同时发生,即多个线程交替运行指令 2. 多线程3种实现方式 2.1 直接创建线程对象 /*** 方式1:* 1. 创建thread类的…...

你敢信?软件测试万能面试脚本他来了?
写在前面: 过春节了,四舍五入接下来马上要年底了,新一波的跳槽旺季马上来临,不知道你是不是已经安于现状,还是蓄势待发呢?最近我和我的哈皮群友们一顿讨论,拟写了大家可能会遇到的面试情况&…...
C++/Python简单练手题
前言 最近需要开始使用python,但是对python了解的并不多,于是先从很早之前刚学C时写过的一些练手题开始,使用python来实现相同的功能,在温习python基础语法的同时,也一起来感受感受python的魅力 99乘法表 c…...

视频在线压缩
video2edit 一款免费的在线视频编辑软件,可以进行视频合并、视频剪辑、视频压缩以及转换视频格式等。 链接地址:在线视频编辑器和转换器 - 编辑,转换和压缩视频文件 打开视频压缩页面,上传想要压缩视频,支持MP4&…...

Python列表的合并、重复、判断与切片操作你学会了吗
1.合并列表 通过 实现 list1 ["佛跳墙", "肠粉", "刀削面", "烤鸭"]list2 [32, 4, 5, 7.43, True]list3 list1 list2print(list3) # [佛跳墙, 肠粉, 刀削面, 烤鸭, 32, 4, 5, 7.43, True] 2.重复输出列表中的元素 通过 * 实…...

Vue(3.3.4)+three.js(0.161.0)实现3D可视化地图
一.前言 由于最近在学习three.js,所以观摩了一下掘金,csdn等网站上的有关这部分的内容,刚好看到一个带你入门three.js——从0到1实现一个3d可视化地图 - 掘金 (juejin.cn),再加上我的专业属性是地理相关,可以说是专业对口…...
谷歌浏览器插件
项目中有时候会用到插件 sync-cookie-extension1.0.0:开发环境同步测试 cookie 至 localhost,便于本地请求服务携带 cookie 参考地址:https://juejin.cn/post/7139354571712757767 里面有源码下载下来,加在到扩展即可使用FeHelp…...

解决Ubuntu22.04 VMware失败的问题 ubuntu入门之二十八
现象1 打开VMware失败 Ubuntu升级之后打开VMware上报需要安装vmmon和vmnet,点击确认后如下提示 最终上报fail 解决方法 内核升级导致,需要在新内核下重新下载编译安装 查看版本 $ vmware -v VMware Workstation 17.5.1 build-23298084$ lsb_release…...

深入理解JavaScript设计模式之单例模式
目录 什么是单例模式为什么需要单例模式常见应用场景包括 单例模式实现透明单例模式实现不透明单例模式用代理实现单例模式javaScript中的单例模式使用命名空间使用闭包封装私有变量 惰性单例通用的惰性单例 结语 什么是单例模式 单例模式(Singleton Pattern&#…...

Springcloud:Eureka 高可用集群搭建实战(服务注册与发现的底层原理与避坑指南)
引言:为什么 Eureka 依然是存量系统的核心? 尽管 Nacos 等新注册中心崛起,但金融、电力等保守行业仍有大量系统运行在 Eureka 上。理解其高可用设计与自我保护机制,是保障分布式系统稳定的必修课。本文将手把手带你搭建生产级 Eur…...
WEB3全栈开发——面试专业技能点P2智能合约开发(Solidity)
一、Solidity合约开发 下面是 Solidity 合约开发 的概念、代码示例及讲解,适合用作学习或写简历项目背景说明。 🧠 一、概念简介:Solidity 合约开发 Solidity 是一种专门为 以太坊(Ethereum)平台编写智能合约的高级编…...

Android15默认授权浮窗权限
我们经常有那种需求,客户需要定制的apk集成在ROM中,并且默认授予其【显示在其他应用的上层】权限,也就是我们常说的浮窗权限,那么我们就可以通过以下方法在wms、ams等系统服务的systemReady()方法中调用即可实现预置应用默认授权浮…...
精益数据分析(97/126):邮件营销与用户参与度的关键指标优化指南
精益数据分析(97/126):邮件营销与用户参与度的关键指标优化指南 在数字化营销时代,邮件列表效度、用户参与度和网站性能等指标往往决定着创业公司的增长成败。今天,我们将深入解析邮件打开率、网站可用性、页面参与时…...
return this;返回的是谁
一个审批系统的示例来演示责任链模式的实现。假设公司需要处理不同金额的采购申请,不同级别的经理有不同的审批权限: // 抽象处理者:审批者 abstract class Approver {protected Approver successor; // 下一个处理者// 设置下一个处理者pub…...
Go 语言并发编程基础:无缓冲与有缓冲通道
在上一章节中,我们了解了 Channel 的基本用法。本章将重点分析 Go 中通道的两种类型 —— 无缓冲通道与有缓冲通道,它们在并发编程中各具特点和应用场景。 一、通道的基本分类 类型定义形式特点无缓冲通道make(chan T)发送和接收都必须准备好࿰…...
【无标题】路径问题的革命性重构:基于二维拓扑收缩色动力学模型的零点隧穿理论
路径问题的革命性重构:基于二维拓扑收缩色动力学模型的零点隧穿理论 一、传统路径模型的根本缺陷 在经典正方形路径问题中(图1): mermaid graph LR A((A)) --- B((B)) B --- C((C)) C --- D((D)) D --- A A -.- C[无直接路径] B -…...