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.单表查询

  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. 按照部门号升序、雇员的工资降序来排序

    # 需求 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)
    
  3. 使用年薪(年薪=月薪*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. 显示工资最高的员工的名字和工作岗位

    # 需求 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. 显示工资高于平均工资的员工信息

    # 需求 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. 显示每个部门的平均工资和最高工资

    # 需求 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)
    
  7. 显示平均工资低于 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. 显示每种岗位的雇员总数,平均工资

    # 需求 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. 显示雇员名、雇员工资以及所在部门的名字

    # 需求 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)
    
  2. 显示部门号为 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. 显示各个员工的姓名,工资,及工资级别

    # 需求 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)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/716270.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

课程表系列(BFS)

广度优先搜索 文章目录 广度优先搜索207. 课程表210. 课程表 II思路 630. 课程表 III1462. 课程表 IV547. 省份数量 207. 课程表 207. 课程表 你这个学期必须选修 numCourses 门课程&#xff0c;记为 0 到 numCourses - 1 。 在选修某些课程之前需要一些先修课程。 先修课程…

c++11 标准模板(STL)(std::tuple)(三)

定义于头文件 <tuple> template< class... Types > class tuple; (C11 起) 类模板 std::tuple 是固定大小的异类值汇集。它是 std::pair 的推广。 若 (std::is_trivially_destructible_v<Types> && ...) 为 true &#xff0c;则 tuple 的析构函数是…

【AI绘画】免费GPU Tesla A100 32G算力部署Stable Diffusion

免责声明 在阅读和实践本文提供的内容之前&#xff0c;请注意以下免责声明&#xff1a; 侵权问题: 本文提供的信息仅供学习参考&#xff0c;不用做任何商业用途&#xff0c;如造成侵权&#xff0c;请私信我&#xff0c;我会立即删除&#xff0c;作者不对读者因使用本文所述方法…

Matlab 机器人工具箱 RobotArm类

文章目录 1 RobotArm1.1 方法1.2 注意2 RobotArm.RobotArm3 RobotArm.cmove4 其他官网:Robotics Toolbox - Peter Corke 1 RobotArm 串联机械臂类 1.1 方法 方法描述plot显示机器人的图形表示teach驱动物理和图形机器人mirror使用机器人作为从机来驱动图形</

深入了解Kafka的文件存储原理

Kafka简介 Kafka最初由Linkedin公司开发的分布式、分区的、多副本的、多订阅者的消息系统。它提供了类似于JMS的特性&#xff0c;但是在设计实现上完全不同&#xff0c;此外它并不是JMS规范的实现。kafka对消息保存是根据Topic进行归类&#xff0c;发送消息者称为Producer&…

IntelliJ IDEA 常用的插件

IntelliJ IDEA有很多常用的插件&#xff0c;这些插件可以扩展IDE的功能&#xff0c;提高开发效率。以下是一些常用的插件&#xff1a; Maven Helper&#xff1a;这是一款分析Maven依赖冲突的插件。在没有此插件时&#xff0c;查看Maven的依赖树和检查依赖包冲突可能需要输入命…

梯度下降算法(带你 原理 实践)

目录 一、引言 二、梯度下降算法的原理 三、梯度下降算法的实现 四、梯度下降算法的优缺点 优点&#xff1a; 缺点&#xff1a; 五、梯度下降算法的改进策略 1 随机梯度下降&#xff08;Stochastic Gradient Descent, SGD&#xff09; 2 批量梯度下降&#xff08;Batch…

LLM分布式训练第一课(通讯原语)

这个系列作为TFLOPS和显存消耗的续篇,今天开始正式连载 上一部地址: LLM 参数,显存,Tflops? 训练篇(5) (qq.com) 前一篇文章举了65B模型的训练所消耗的显存的案例,如果把条件降低一点,我们看一下7B的模型需要多少显存? 2byte的模型静态参数权重(以16bit存储) = 1…

(一)Python数据分析体系--九五小庞

课程地址&#xff1a;https://space.bilibili.com/387143299/channel/collectiondetail?sid554734 主要内容 知识体系 分析什么样的数据 为什么使用Python做数据分析 Python近几年的发展势头是有目共睹的&#xff0c;尤其是在科学计算&#xff0c;数据处理&#xff0c;A方面…

驾辰龙跨Llama持Wasm,玩转Yi模型迎新春

今年新年很特别&#xff0c;AI工具添光彩。今天就来感受下最新的AI神器天选组合“WasmEdgeYi-34B”&#xff0c;只要短短三步&#xff0c;为这个甲辰龙年带来一份九紫离火运的科技感。 环境准备 这次用的算力是OpenBayes提供的英伟达RTX_4090*1、24GB显存、20核CPU、80GB内存…

产品营销展示型wordpress外贸网站模板

工艺品wordpress外贸主题 简约大气的wordpress外贸主题&#xff0c;适合做工艺品进出品外贸的公司官网使用。 https://www.jianzhanpress.com/?p5377 餐饮设备wordpress外贸主题 简洁的wordpress外贸主题&#xff0c;适合食品机械、餐饮设备公司使用。 https://www.jianzh…

Linux 开发工具vim、gcc/g++、makefile

目录 Linux编辑器-vim 1. 基本概念 2. 基本操作 3. 正常模式命令集 4. 末行模式命令集 5. 其他操作 6. 简单vim配置 Linux编译器-gcc/g 1、基本概念 2、程序翻译的过程 3. gcc如何完成程序翻译 4、动静态库 Linux项目自动化构建工具-make/Makefile 1、背景 2、…

【Qt学习笔记】(四)Qt窗口

Qt窗口 1 菜单栏1.1 创建菜单栏1.2 在菜单栏中添加菜单1.3 创建菜单项1.4 在菜单项之间添加分割线1.5 给菜单项添加槽函数1.6 给菜单项添加快捷键 2 工具栏2.1 创建工具栏2.2 设置停靠位置2.3 设置浮动属性2.4 设置移动属性2.5 添加 Action 3 状态栏3.1 状态栏的创建3.2 在状态…

2024最新算法:冠豪猪优化算法(CPO)求解23个基准函数

一、冠豪猪优化算法 冠豪猪优化算法(Crested Porcupine Optimizer&#xff0c;CPO)由Mohamed Abdel-Basset等人于2024年提出&#xff0c;该算法模拟冠豪猪的四种不同保护机制&#xff1a;视觉、听觉、气味和物理攻击。第一和第二防御技术&#xff08;视觉和听觉&#xff09;反…

盘点 | IT行业哪些认证含金量高

微思网络 厦门微思网络 作为一名IT人员&#xff0c;谁没考几个证 ——值得考的证书拥有的特性 ▶ 获政府、企业和从业者认可&#xff1b; ▶ 持证人数多&#xff0c;业内共识度高&#xff1b; ▶ 帮持证者加分&#xff0c;快速提薪。 系统网络方向认证 01 华为认证 华为…

设计模式学习笔记 - 设计原则 - 7.DRY 原则及提高代码复用性

前言 DRY 原则&#xff0c;英文描述为&#xff1a; Don’t Repeat Yourself。中文直译&#xff1a;不要重复自己。将它应用在编程中&#xff0c;可理解为&#xff1a;不要写重读的代码。 可能你认为&#xff0c;这个原则很简单。只要两段代码长得一样&#xff0c;那就是违反 …

【机器学习】包裹式特征选择之递归特征消除法

&#x1f388;个人主页&#xff1a;豌豆射手^ &#x1f389;欢迎 &#x1f44d;点赞✍评论⭐收藏 &#x1f917;收录专栏&#xff1a;机器学习 &#x1f91d;希望本文对您有所裨益&#xff0c;如有不足之处&#xff0c;欢迎在评论区提出指正&#xff0c;让我们共同学习、交流进…

电磁兼容(EMC):电解电容低阻如何选择详解

目录 1 为何要选低阻电解电容 2 电解电容等效高频等效电路 3 不同厂家ESR参数 4 高频ESR特性 5 Low ESR铝电解电容 1 为何要选低阻电解电容 在EMI超标时&#xff0c;将普通电解电容更换为低阻电解电容时&#xff0c;便通过了。这是因为低阻电解电容降低了功率回路的辐射电…

数字化转型导师坚鹏:证券公司数字化转型战略、方法与案例

证券公司数字化转型战略、方法与案例 课程背景&#xff1a; 数字化转型背景下&#xff0c;很多机构存在以下问题&#xff1a; 不清楚证券公司数字化转型的发展战略&#xff1f; 不知道证券公司数字化转型的核心方法&#xff1f; 不知道证券公司数字化转型的成功案例&am…

LLM 系列——BERT——论文解读

一、概述 1、是什么 是单模态“小”语言模型&#xff0c;是一个“Bidirectional Encoder Representations fromTransformers”的缩写&#xff0c;是一个语言预训练模型&#xff0c;通过随机掩盖一些词&#xff0c;然后预测这些被遮盖的词来训练双向语言模型&#xff08;编码器…