• 【六】【SQL】多表查询,笛卡尔积


    笛卡尔积

    笛卡尔积发生在当你在查询中将两个或多个表进行交叉连接(CROSS JOIN)或者没有指定任何连接条件时。假设第一个表有M行,第二个表有N行,那么结果集将包含M x N个记录。在大多数情况下,笛卡尔积并不是你想要的结果,因为它会产生大量的无关数据。但是,理解它是如何发生的对于避免无意中产生笛卡尔积是很有帮助的。

    显示雇员名、雇员工资以及所在部门的名字

    因为上面的数据来自于EMP和DEPT表,因此要联合查询

     
    
    1. mysql> select *from emp;
    2. +--------+--------+-----------+------+---------------------+---------+---------+--------+
    3. | empno | ename | job | mgr | hiredate | sal | comm | deptno |
    4. +--------+--------+-----------+------+---------------------+---------+---------+--------+
    5. | 007369 | SMITH | CLERK | 7902 | 1980-12-17 00:00:00 | 800.00 | NULL | 20 |
    6. | 007499 | ALLEN | SALESMAN | 7698 | 1981-02-20 00:00:00 | 1600.00 | 300.00 | 30 |
    7. | 007521 | WARD | SALESMAN | 7698 | 1981-02-22 00:00:00 | 1250.00 | 500.00 | 30 |
    8. | 007566 | JONES | MANAGER | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL | 20 |
    9. | 007654 | MARTIN | SALESMAN | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 | 30 |
    10. | 007698 | BLAKE | MANAGER | 7839 | 1981-05-01 00:00:00 | 2850.00 | NULL | 30 |
    11. | 007782 | CLARK | MANAGER | 7839 | 1981-06-09 00:00:00 | 2450.00 | NULL | 10 |
    12. | 007788 | SCOTT | ANALYST | 7566 | 1987-04-19 00:00:00 | 3000.00 | NULL | 20 |
    13. | 007839 | KING | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000.00 | NULL | 10 |
    14. | 007844 | TURNER | SALESMAN | 7698 | 1981-09-08 00:00:00 | 1500.00 | 0.00 | 30 |
    15. | 007876 | ADAMS | CLERK | 7788 | 1987-05-23 00:00:00 | 1100.00 | NULL | 20 |
    16. | 007900 | JAMES | CLERK | 7698 | 1981-12-03 00:00:00 | 950.00 | NULL | 30 |
    17. | 007902 | FORD | ANALYST | 7566 | 1981-12-03 00:00:00 | 3000.00 | NULL | 20 |
    18. | 007934 | MILLER | CLERK | 7782 | 1982-01-23 00:00:00 | 1300.00 | NULL | 10 |
    19. +--------+--------+-----------+------+---------------------+---------+---------+--------+
    20. 14 rows in set (0.00 sec)
    21. mysql> select *from dept;
    22. +--------+------------+----------+
    23. | deptno | dname | loc |
    24. +--------+------------+----------+
    25. | 10 | ACCOUNTING | NEW YORK |
    26. | 20 | RESEARCH | DALLAS |
    27. | 30 | SALES | CHICAGO |
    28. | 40 | OPERATIONS | BOSTON |
    29. +--------+------------+----------+
    30. 4 rows in set (0.00 sec)
    31. mysql> select *from emp,dept;
    32. +--------+--------+-----------+------+---------------------+---------+---------+--------+--------+------------+----------+
    33. | empno | ename | job | mgr | hiredate | sal | comm | deptno | deptno | dname | loc |
    34. +--------+--------+-----------+------+---------------------+---------+---------+--------+--------+------------+----------+
    35. | 007369 | SMITH | CLERK | 7902 | 1980-12-17 00:00:00 | 800.00 | NULL | 20 | 10 | ACCOUNTING | NEW YORK |
    36. | 007369 | SMITH | CLERK | 7902 | 1980-12-17 00:00:00 | 800.00 | NULL | 20 | 20 | RESEARCH | DALLAS |
    37. | 007369 | SMITH | CLERK | 7902 | 1980-12-17 00:00:00 | 800.00 | NULL | 20 | 30 | SALES | CHICAGO |
    38. | 007369 | SMITH | CLERK | 7902 | 1980-12-17 00:00:00 | 800.00 | NULL | 20 | 40 | OPERATIONS | BOSTON |
    39. | 007499 | ALLEN | SALESMAN | 7698 | 1981-02-20 00:00:00 | 1600.00 | 300.00 | 30 | 10 | ACCOUNTING | NEW YORK |
    40. | 007499 | ALLEN | SALESMAN | 7698 | 1981-02-20 00:00:00 | 1600.00 | 300.00 | 30 | 20 | RESEARCH | DALLAS |
    41. | 007499 | ALLEN | SALESMAN | 7698 | 1981-02-20 00:00:00 | 1600.00 | 300.00 | 30 | 30 | SALES | CHICAGO |
    42. | 007499 | ALLEN | SALESMAN | 7698 | 1981-02-20 00:00:00 | 1600.00 | 300.00 | 30 | 40 | OPERATIONS | BOSTON |
    43. | 007521 | WARD | SALESMAN | 7698 | 1981-02-22 00:00:00 | 1250.00 | 500.00 | 30 | 10 | ACCOUNTING | NEW YORK |
    44. | 007521 | WARD | SALESMAN | 7698 | 1981-02-22 00:00:00 | 1250.00 | 500.00 | 30 | 20 | RESEARCH | DALLAS |
    45. | 007521 | WARD | SALESMAN | 7698 | 1981-02-22 00:00:00 | 1250.00 | 500.00 | 30 | 30 | SALES | CHICAGO |
    46. | 007521 | WARD | SALESMAN | 7698 | 1981-02-22 00:00:00 | 1250.00 | 500.00 | 30 | 40 | OPERATIONS | BOSTON |
    47. | 007566 | JONES | MANAGER | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL | 20 | 10 | ACCOUNTING | NEW YORK |
    48. | 007566 | JONES | MANAGER | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL | 20 | 20 | RESEARCH | DALLAS |
    49. | 007566 | JONES | MANAGER | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL | 20 | 30 | SALES | CHICAGO |
    50. | 007566 | JONES | MANAGER | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL | 20 | 40 | OPERATIONS | BOSTON |
    51. | 007654 | MARTIN | SALESMAN | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 | 30 | 10 | ACCOUNTING | NEW YORK |
    52. | 007654 | MARTIN | SALESMAN | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 | 30 | 20 | RESEARCH | DALLAS |
    53. | 007654 | MARTIN | SALESMAN | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 | 30 | 30 | SALES | CHICAGO |
    54. | 007654 | MARTIN | SALESMAN | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 | 30 | 40 | OPERATIONS | BOSTON |
    55. | 007698 | BLAKE | MANAGER | 7839 | 1981-05-01 00:00:00 | 2850.00 | NULL | 30 | 10 | ACCOUNTING | NEW YORK |
    56. | 007698 | BLAKE | MANAGER | 7839 | 1981-05-01 00:00:00 | 2850.00 | NULL | 30 | 20 | RESEARCH | DALLAS |
    57. | 007698 | BLAKE | MANAGER | 7839 | 1981-05-01 00:00:00 | 2850.00 | NULL | 30 | 30 | SALES | CHICAGO |
    58. | 007698 | BLAKE | MANAGER | 7839 | 1981-05-01 00:00:00 | 2850.00 | NULL | 30 | 40 | OPERATIONS | BOSTON |
    59. | 007782 | CLARK | MANAGER | 7839 | 1981-06-09 00:00:00 | 2450.00 | NULL | 10 | 10 | ACCOUNTING | NEW YORK |
    60. | 007782 | CLARK | MANAGER | 7839 | 1981-06-09 00:00:00 | 2450.00 | NULL | 10 | 20 | RESEARCH | DALLAS |
    61. | 007782 | CLARK | MANAGER | 7839 | 1981-06-09 00:00:00 | 2450.00 | NULL | 10 | 30 | SALES | CHICAGO |
    62. | 007782 | CLARK | MANAGER | 7839 | 1981-06-09 00:00:00 | 2450.00 | NULL | 10 | 40 | OPERATIONS | BOSTON |
    63. | 007788 | SCOTT | ANALYST | 7566 | 1987-04-19 00:00:00 | 3000.00 | NULL | 20 | 10 | ACCOUNTING | NEW YORK |
    64. | 007788 | SCOTT | ANALYST | 7566 | 1987-04-19 00:00:00 | 3000.00 | NULL | 20 | 20 | RESEARCH | DALLAS |
    65. | 007788 | SCOTT | ANALYST | 7566 | 1987-04-19 00:00:00 | 3000.00 | NULL | 20 | 30 | SALES | CHICAGO |
    66. | 007788 | SCOTT | ANALYST | 7566 | 1987-04-19 00:00:00 | 3000.00 | NULL | 20 | 40 | OPERATIONS | BOSTON |
    67. | 007839 | KING | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000.00 | NULL | 10 | 10 | ACCOUNTING | NEW YORK |
    68. | 007839 | KING | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000.00 | NULL | 10 | 20 | RESEARCH | DALLAS |
    69. | 007839 | KING | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000.00 | NULL | 10 | 30 | SALES | CHICAGO |
    70. | 007839 | KING | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000.00 | NULL | 10 | 40 | OPERATIONS | BOSTON |
    71. | 007844 | TURNER | SALESMAN | 7698 | 1981-09-08 00:00:00 | 1500.00 | 0.00 | 30 | 10 | ACCOUNTING | NEW YORK |
    72. | 007844 | TURNER | SALESMAN | 7698 | 1981-09-08 00:00:00 | 1500.00 | 0.00 | 30 | 20 | RESEARCH | DALLAS |
    73. | 007844 | TURNER | SALESMAN | 7698 | 1981-09-08 00:00:00 | 1500.00 | 0.00 | 30 | 30 | SALES | CHICAGO |
    74. | 007844 | TURNER | SALESMAN | 7698 | 1981-09-08 00:00:00 | 1500.00 | 0.00 | 30 | 40 | OPERATIONS | BOSTON |
    75. | 007876 | ADAMS | CLERK | 7788 | 1987-05-23 00:00:00 | 1100.00 | NULL | 20 | 10 | ACCOUNTING | NEW YORK |
    76. | 007876 | ADAMS | CLERK | 7788 | 1987-05-23 00:00:00 | 1100.00 | NULL | 20 | 20 | RESEARCH | DALLAS |
    77. | 007876 | ADAMS | CLERK | 7788 | 1987-05-23 00:00:00 | 1100.00 | NULL | 20 | 30 | SALES | CHICAGO |
    78. | 007876 | ADAMS | CLERK | 7788 | 1987-05-23 00:00:00 | 1100.00 | NULL | 20 | 40 | OPERATIONS | BOSTON |
    79. | 007900 | JAMES | CLERK | 7698 | 1981-12-03 00:00:00 | 950.00 | NULL | 30 | 10 | ACCOUNTING | NEW YORK |
    80. | 007900 | JAMES | CLERK | 7698 | 1981-12-03 00:00:00 | 950.00 | NULL | 30 | 20 | RESEARCH | DALLAS |
    81. | 007900 | JAMES | CLERK | 7698 | 1981-12-03 00:00:00 | 950.00 | NULL | 30 | 30 | SALES | CHICAGO |
    82. | 007900 | JAMES | CLERK | 7698 | 1981-12-03 00:00:00 | 950.00 | NULL | 30 | 40 | OPERATIONS | BOSTON |
    83. | 007902 | FORD | ANALYST | 7566 | 1981-12-03 00:00:00 | 3000.00 | NULL | 20 | 10 | ACCOUNTING | NEW YORK |
    84. | 007902 | FORD | ANALYST | 7566 | 1981-12-03 00:00:00 | 3000.00 | NULL | 20 | 20 | RESEARCH | DALLAS |
    85. | 007902 | FORD | ANALYST | 7566 | 1981-12-03 00:00:00 | 3000.00 | NULL | 20 | 30 | SALES | CHICAGO |
    86. | 007902 | FORD | ANALYST | 7566 | 1981-12-03 00:00:00 | 3000.00 | NULL | 20 | 40 | OPERATIONS | BOSTON |
    87. | 007934 | MILLER | CLERK | 7782 | 1982-01-23 00:00:00 | 1300.00 | NULL | 10 | 10 | ACCOUNTING | NEW YORK |
    88. | 007934 | MILLER | CLERK | 7782 | 1982-01-23 00:00:00 | 1300.00 | NULL | 10 | 20 | RESEARCH | DALLAS |
    89. | 007934 | MILLER | CLERK | 7782 | 1982-01-23 00:00:00 | 1300.00 | NULL | 10 | 30 | SALES | CHICAGO |
    90. | 007934 | MILLER | CLERK | 7782 | 1982-01-23 00:00:00 | 1300.00 | NULL | 10 | 40 | OPERATIONS | BOSTON |
    91. +--------+--------+-----------+------+---------------------+---------+---------+--------+--------+------------+----------+
    92. 56 rows in set (0.02 sec)
    93. mysql> select *from emp,dept where emp.deptno=dept.deptno;
    94. +--------+--------+-----------+------+---------------------+---------+---------+--------+--------+------------+----------+
    95. | empno | ename | job | mgr | hiredate | sal | comm | deptno | deptno | dname | loc |
    96. +--------+--------+-----------+------+---------------------+---------+---------+--------+--------+------------+----------+
    97. | 007369 | SMITH | CLERK | 7902 | 1980-12-17 00:00:00 | 800.00 | NULL | 20 | 20 | RESEARCH | DALLAS |
    98. | 007499 | ALLEN | SALESMAN | 7698 | 1981-02-20 00:00:00 | 1600.00 | 300.00 | 30 | 30 | SALES | CHICAGO |
    99. | 007521 | WARD | SALESMAN | 7698 | 1981-02-22 00:00:00 | 1250.00 | 500.00 | 30 | 30 | SALES | CHICAGO |
    100. | 007566 | JONES | MANAGER | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL | 20 | 20 | RESEARCH | DALLAS |
    101. | 007654 | MARTIN | SALESMAN | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 | 30 | 30 | SALES | CHICAGO |
    102. | 007698 | BLAKE | MANAGER | 7839 | 1981-05-01 00:00:00 | 2850.00 | NULL | 30 | 30 | SALES | CHICAGO |
    103. | 007782 | CLARK | MANAGER | 7839 | 1981-06-09 00:00:00 | 2450.00 | NULL | 10 | 10 | ACCOUNTING | NEW YORK |
    104. | 007788 | SCOTT | ANALYST | 7566 | 1987-04-19 00:00:00 | 3000.00 | NULL | 20 | 20 | RESEARCH | DALLAS |
    105. | 007839 | KING | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000.00 | NULL | 10 | 10 | ACCOUNTING | NEW YORK |
    106. | 007844 | TURNER | SALESMAN | 7698 | 1981-09-08 00:00:00 | 1500.00 | 0.00 | 30 | 30 | SALES | CHICAGO |
    107. | 007876 | ADAMS | CLERK | 7788 | 1987-05-23 00:00:00 | 1100.00 | NULL | 20 | 20 | RESEARCH | DALLAS |
    108. | 007900 | JAMES | CLERK | 7698 | 1981-12-03 00:00:00 | 950.00 | NULL | 30 | 30 | SALES | CHICAGO |
    109. | 007902 | FORD | ANALYST | 7566 | 1981-12-03 00:00:00 | 3000.00 | NULL | 20 | 20 | RESEARCH | DALLAS |
    110. | 007934 | MILLER | CLERK | 7782 | 1982-01-23 00:00:00 | 1300.00 | NULL | 10 | 10 | ACCOUNTING | NEW YORK |
    111. +--------+--------+-----------+------+---------------------+---------+---------+--------+--------+------------+----------+
    112. 14 rows in set (0.00 sec)
    113. mysql> select ename,sal,dname from emp,dept where emp.deptno=dept.deptno;
    114. +--------+---------+------------+
    115. | ename | sal | dname |
    116. +--------+---------+------------+
    117. | SMITH | 800.00 | RESEARCH |
    118. | ALLEN | 1600.00 | SALES |
    119. | WARD | 1250.00 | SALES |
    120. | JONES | 2975.00 | RESEARCH |
    121. | MARTIN | 1250.00 | SALES |
    122. | BLAKE | 2850.00 | SALES |
    123. | CLARK | 2450.00 | ACCOUNTING |
    124. | SCOTT | 3000.00 | RESEARCH |
    125. | KING | 5000.00 | ACCOUNTING |
    126. | TURNER | 1500.00 | SALES |
    127. | ADAMS | 1100.00 | RESEARCH |
    128. | JAMES | 950.00 | SALES |
    129. | FORD | 3000.00 | RESEARCH |
    130. | MILLER | 1300.00 | ACCOUNTING |
    131. +--------+---------+------------+
    132. 14 rows in set (0.00 sec)
    133. mysql>

    这些MySQL查询示例展示了如何从emp(员工)和dept(部门)两个表中进行数据检索,以及如何通过连接条件避免生成笛卡尔积,从而获取有意义的结果。

    简单的多表查询(产生笛卡尔积): 当你执行select * from emp,dept;时,没有指定连接条件,因此结果是emp表和dept表之间的笛卡尔积。每个emp表中的行都会与dept表中的每行组合,如果emp表有14行,dept表有4行,那么结果集将有56行(14*4)。

    指定连接条件的多表查询: 通过添加where emp.deptno=dept.deptno;,你明确指定了连接条件,即只有当员工所在部门编号(deptno)与部门表中的部门编号相匹配时,相应的记录才会出现在结果集中。这种方式有效地避免了笛卡尔积,只返回了相关联的记录,即每个员工及其对应的部门信息。

    特定字段选择: 最后一个查询select ename,sal,dname from emp,dept where emp.deptno=dept.deptno;进一步细化了输出,只选择了员工的姓名(ename)、薪水(sal)和部门名称(dname)。通过这种方式,可以清晰地看到每个员工所属的部门及其薪水,而不是返回两个表的所有列,这使得结果更加简洁和有用。

    显示部门号为10的部门名,员工名和工资

     
    
    1. mysql> select *from emp,dept where emp.deptno=dept.deptno;
    2. +--------+--------+-----------+------+---------------------+---------+---------+--------+--------+------------+----------+
    3. | empno | ename | job | mgr | hiredate | sal | comm | deptno | deptno | dname | loc |
    4. +--------+--------+-----------+------+---------------------+---------+---------+--------+--------+------------+----------+
    5. | 007369 | SMITH | CLERK | 7902 | 1980-12-17 00:00:00 | 800.00 | NULL | 20 | 20 | RESEARCH | DALLAS |
    6. | 007499 | ALLEN | SALESMAN | 7698 | 1981-02-20 00:00:00 | 1600.00 | 300.00 | 30 | 30 | SALES | CHICAGO |
    7. | 007521 | WARD | SALESMAN | 7698 | 1981-02-22 00:00:00 | 1250.00 | 500.00 | 30 | 30 | SALES | CHICAGO |
    8. | 007566 | JONES | MANAGER | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL | 20 | 20 | RESEARCH | DALLAS |
    9. | 007654 | MARTIN | SALESMAN | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 | 30 | 30 | SALES | CHICAGO |
    10. | 007698 | BLAKE | MANAGER | 7839 | 1981-05-01 00:00:00 | 2850.00 | NULL | 30 | 30 | SALES | CHICAGO |
    11. | 007782 | CLARK | MANAGER | 7839 | 1981-06-09 00:00:00 | 2450.00 | NULL | 10 | 10 | ACCOUNTING | NEW YORK |
    12. | 007788 | SCOTT | ANALYST | 7566 | 1987-04-19 00:00:00 | 3000.00 | NULL | 20 | 20 | RESEARCH | DALLAS |
    13. | 007839 | KING | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000.00 | NULL | 10 | 10 | ACCOUNTING | NEW YORK |
    14. | 007844 | TURNER | SALESMAN | 7698 | 1981-09-08 00:00:00 | 1500.00 | 0.00 | 30 | 30 | SALES | CHICAGO |
    15. | 007876 | ADAMS | CLERK | 7788 | 1987-05-23 00:00:00 | 1100.00 | NULL | 20 | 20 | RESEARCH | DALLAS |
    16. | 007900 | JAMES | CLERK | 7698 | 1981-12-03 00:00:00 | 950.00 | NULL | 30 | 30 | SALES | CHICAGO |
    17. | 007902 | FORD | ANALYST | 7566 | 1981-12-03 00:00:00 | 3000.00 | NULL | 20 | 20 | RESEARCH | DALLAS |
    18. | 007934 | MILLER | CLERK | 7782 | 1982-01-23 00:00:00 | 1300.00 | NULL | 10 | 10 | ACCOUNTING | NEW YORK |
    19. +--------+--------+-----------+------+---------------------+---------+---------+--------+--------+------------+----------+
    20. 14 rows in set (0.00 sec)
    21. mysql> select ename,sal,dname from emp,dept where emp.deptno=dept.deptno;
    22. +--------+---------+------------+
    23. | ename | sal | dname |
    24. +--------+---------+------------+
    25. | SMITH | 800.00 | RESEARCH |
    26. | ALLEN | 1600.00 | SALES |
    27. | WARD | 1250.00 | SALES |
    28. | JONES | 2975.00 | RESEARCH |
    29. | MARTIN | 1250.00 | SALES |
    30. | BLAKE | 2850.00 | SALES |
    31. | CLARK | 2450.00 | ACCOUNTING |
    32. | SCOTT | 3000.00 | RESEARCH |
    33. | KING | 5000.00 | ACCOUNTING |
    34. | TURNER | 1500.00 | SALES |
    35. | ADAMS | 1100.00 | RESEARCH |
    36. | JAMES | 950.00 | SALES |
    37. | FORD | 3000.00 | RESEARCH |
    38. | MILLER | 1300.00 | ACCOUNTING |
    39. +--------+---------+------------+
    40. 14 rows in set (0.00 sec)
    41. mysql> select ename,sal,dname from emp,dept where emp.deptno=dept.deptno and emp.deptno=10;
    42. +--------+---------+------------+
    43. | ename | sal | dname |
    44. +--------+---------+------------+
    45. | CLARK | 2450.00 | ACCOUNTING |
    46. | KING | 5000.00 | ACCOUNTING |
    47. | MILLER | 1300.00 | ACCOUNTING |
    48. +--------+---------+------------+
    49. 3 rows in set (0.00 sec)
    50. mysql> select ename,sal,dname,deptno from emp,dept where emp.deptno=dept.deptno and emp.deptno=10;
    51. ERROR 1052 (23000): Column 'deptno' in field list is ambiguous
    52. mysql> select ename,sal,dname,emp.deptno from emp,dept where emp.deptno=dept.deptno and emp.deptno=10;
    53. +--------+---------+------------+--------+
    54. | ename | sal | dname | deptno |
    55. +--------+---------+------------+--------+
    56. | CLARK | 2450.00 | ACCOUNTING | 10 |
    57. | KING | 5000.00 | ACCOUNTING | 10 |
    58. | MILLER | 1300.00 | ACCOUNTING | 10 |
    59. +--------+---------+------------+--------+
    60. 3 rows in set (0.00 sec)
    61. mysql>

    在这些MySQL查询中,我们看到了如何通过连接两个表(empdept)来获取员工的信息以及他们所属的部门信息。最后两个查询特别展示了如何解决列名冲突和如何正确地引用列名以避免错误。

    解决列名冲突: 在执行多表查询时,如果两个表中存在同名的列(在这个例子中是deptno),直接引用这个列名会导致“列名是模糊的”(ambiguous)错误。这是因为SQL无法判断你希望选择哪个表中的deptno列。

    错误示例: 查询select ename,sal,dname,deptno from emp,dept where emp.deptno=dept.deptno and emp.deptno=10;失败并报错ERROR 1052 (23000): Column 'deptno' in field list is ambiguous。这是因为deptno同时存在于empdept表中,而查询没有指明应该从哪个表中选择该列。

    解决方法: 通过在列名前加上表名或别名来解决这个问题。如select ename,sal,dname,emp.deptno from emp,dept where emp.deptno=dept.deptno and emp.deptno=10;明确指出选择emp.deptno,从而解决了列名冲突问题,并成功执行了查询。

    显示每个员工的姓名、工资和工资级别

     
    
    1. mysql> select *from salgrade;
    2. +-------+-------+-------+
    3. | grade | losal | hisal |
    4. +-------+-------+-------+
    5. | 1 | 700 | 1200 |
    6. | 2 | 1201 | 1400 |
    7. | 3 | 1401 | 2000 |
    8. | 4 | 2001 | 3000 |
    9. | 5 | 3001 | 9999 |
    10. +-------+-------+-------+
    11. 5 rows in set (0.00 sec)
    12. mysql> select *from emp,salgrade;
    13. +--------+--------+-----------+------+---------------------+---------+---------+--------+-------+-------+-------+
    14. | empno | ename | job | mgr | hiredate | sal | comm | deptno | grade | losal | hisal |
    15. +--------+--------+-----------+------+---------------------+---------+---------+--------+-------+-------+-------+
    16. | 007369 | SMITH | CLERK | 7902 | 1980-12-17 00:00:00 | 800.00 | NULL | 20 | 1 | 700 | 1200 |
    17. | 007369 | SMITH | CLERK | 7902 | 1980-12-17 00:00:00 | 800.00 | NULL | 20 | 2 | 1201 | 1400 |
    18. | 007369 | SMITH | CLERK | 7902 | 1980-12-17 00:00:00 | 800.00 | NULL | 20 | 3 | 1401 | 2000 |
    19. | 007369 | SMITH | CLERK | 7902 | 1980-12-17 00:00:00 | 800.00 | NULL | 20 | 4 | 2001 | 3000 |
    20. | 007369 | SMITH | CLERK | 7902 | 1980-12-17 00:00:00 | 800.00 | NULL | 20 | 5 | 3001 | 9999 |
    21. | 007499 | ALLEN | SALESMAN | 7698 | 1981-02-20 00:00:00 | 1600.00 | 300.00 | 30 | 1 | 700 | 1200 |
    22. | 007499 | ALLEN | SALESMAN | 7698 | 1981-02-20 00:00:00 | 1600.00 | 300.00 | 30 | 2 | 1201 | 1400 |
    23. | 007499 | ALLEN | SALESMAN | 7698 | 1981-02-20 00:00:00 | 1600.00 | 300.00 | 30 | 3 | 1401 | 2000 |
    24. | 007499 | ALLEN | SALESMAN | 7698 | 1981-02-20 00:00:00 | 1600.00 | 300.00 | 30 | 4 | 2001 | 3000 |
    25. | 007499 | ALLEN | SALESMAN | 7698 | 1981-02-20 00:00:00 | 1600.00 | 300.00 | 30 | 5 | 3001 | 9999 |
    26. | 007521 | WARD | SALESMAN | 7698 | 1981-02-22 00:00:00 | 1250.00 | 500.00 | 30 | 1 | 700 | 1200 |
    27. | 007521 | WARD | SALESMAN | 7698 | 1981-02-22 00:00:00 | 1250.00 | 500.00 | 30 | 2 | 1201 | 1400 |
    28. | 007521 | WARD | SALESMAN | 7698 | 1981-02-22 00:00:00 | 1250.00 | 500.00 | 30 | 3 | 1401 | 2000 |
    29. | 007521 | WARD | SALESMAN | 7698 | 1981-02-22 00:00:00 | 1250.00 | 500.00 | 30 | 4 | 2001 | 3000 |
    30. | 007521 | WARD | SALESMAN | 7698 | 1981-02-22 00:00:00 | 1250.00 | 500.00 | 30 | 5 | 3001 | 9999 |
    31. | 007566 | JONES | MANAGER | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL | 20 | 1 | 700 | 1200 |
    32. | 007566 | JONES | MANAGER | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL | 20 | 2 | 1201 | 1400 |
    33. | 007566 | JONES | MANAGER | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL | 20 | 3 | 1401 | 2000 |
    34. | 007566 | JONES | MANAGER | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL | 20 | 4 | 2001 | 3000 |
    35. | 007566 | JONES | MANAGER | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL | 20 | 5 | 3001 | 9999 |
    36. | 007654 | MARTIN | SALESMAN | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 | 30 | 1 | 700 | 1200 |
    37. | 007654 | MARTIN | SALESMAN | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 | 30 | 2 | 1201 | 1400 |
    38. | 007654 | MARTIN | SALESMAN | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 | 30 | 3 | 1401 | 2000 |
    39. | 007654 | MARTIN | SALESMAN | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 | 30 | 4 | 2001 | 3000 |
    40. | 007654 | MARTIN | SALESMAN | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 | 30 | 5 | 3001 | 9999 |
    41. | 007698 | BLAKE | MANAGER | 7839 | 1981-05-01 00:00:00 | 2850.00 | NULL | 30 | 1 | 700 | 1200 |
    42. | 007698 | BLAKE | MANAGER | 7839 | 1981-05-01 00:00:00 | 2850.00 | NULL | 30 | 2 | 1201 | 1400 |
    43. | 007698 | BLAKE | MANAGER | 7839 | 1981-05-01 00:00:00 | 2850.00 | NULL | 30 | 3 | 1401 | 2000 |
    44. | 007698 | BLAKE | MANAGER | 7839 | 1981-05-01 00:00:00 | 2850.00 | NULL | 30 | 4 | 2001 | 3000 |
    45. | 007698 | BLAKE | MANAGER | 7839 | 1981-05-01 00:00:00 | 2850.00 | NULL | 30 | 5 | 3001 | 9999 |
    46. | 007782 | CLARK | MANAGER | 7839 | 1981-06-09 00:00:00 | 2450.00 | NULL | 10 | 1 | 700 | 1200 |
    47. | 007782 | CLARK | MANAGER | 7839 | 1981-06-09 00:00:00 | 2450.00 | NULL | 10 | 2 | 1201 | 1400 |
    48. | 007782 | CLARK | MANAGER | 7839 | 1981-06-09 00:00:00 | 2450.00 | NULL | 10 | 3 | 1401 | 2000 |
    49. | 007782 | CLARK | MANAGER | 7839 | 1981-06-09 00:00:00 | 2450.00 | NULL | 10 | 4 | 2001 | 3000 |
    50. | 007782 | CLARK | MANAGER | 7839 | 1981-06-09 00:00:00 | 2450.00 | NULL | 10 | 5 | 3001 | 9999 |
    51. | 007788 | SCOTT | ANALYST | 7566 | 1987-04-19 00:00:00 | 3000.00 | NULL | 20 | 1 | 700 | 1200 |
    52. | 007788 | SCOTT | ANALYST | 7566 | 1987-04-19 00:00:00 | 3000.00 | NULL | 20 | 2 | 1201 | 1400 |
    53. | 007788 | SCOTT | ANALYST | 7566 | 1987-04-19 00:00:00 | 3000.00 | NULL | 20 | 3 | 1401 | 2000 |
    54. | 007788 | SCOTT | ANALYST | 7566 | 1987-04-19 00:00:00 | 3000.00 | NULL | 20 | 4 | 2001 | 3000 |
    55. | 007788 | SCOTT | ANALYST | 7566 | 1987-04-19 00:00:00 | 3000.00 | NULL | 20 | 5 | 3001 | 9999 |
    56. | 007839 | KING | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000.00 | NULL | 10 | 1 | 700 | 1200 |
    57. | 007839 | KING | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000.00 | NULL | 10 | 2 | 1201 | 1400 |
    58. | 007839 | KING | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000.00 | NULL | 10 | 3 | 1401 | 2000 |
    59. | 007839 | KING | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000.00 | NULL | 10 | 4 | 2001 | 3000 |
    60. | 007839 | KING | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000.00 | NULL | 10 | 5 | 3001 | 9999 |
    61. | 007844 | TURNER | SALESMAN | 7698 | 1981-09-08 00:00:00 | 1500.00 | 0.00 | 30 | 1 | 700 | 1200 |
    62. | 007844 | TURNER | SALESMAN | 7698 | 1981-09-08 00:00:00 | 1500.00 | 0.00 | 30 | 2 | 1201 | 1400 |
    63. | 007844 | TURNER | SALESMAN | 7698 | 1981-09-08 00:00:00 | 1500.00 | 0.00 | 30 | 3 | 1401 | 2000 |
    64. | 007844 | TURNER | SALESMAN | 7698 | 1981-09-08 00:00:00 | 1500.00 | 0.00 | 30 | 4 | 2001 | 3000 |
    65. | 007844 | TURNER | SALESMAN | 7698 | 1981-09-08 00:00:00 | 1500.00 | 0.00 | 30 | 5 | 3001 | 9999 |
    66. | 007876 | ADAMS | CLERK | 7788 | 1987-05-23 00:00:00 | 1100.00 | NULL | 20 | 1 | 700 | 1200 |
    67. | 007876 | ADAMS | CLERK | 7788 | 1987-05-23 00:00:00 | 1100.00 | NULL | 20 | 2 | 1201 | 1400 |
    68. | 007876 | ADAMS | CLERK | 7788 | 1987-05-23 00:00:00 | 1100.00 | NULL | 20 | 3 | 1401 | 2000 |
    69. | 007876 | ADAMS | CLERK | 7788 | 1987-05-23 00:00:00 | 1100.00 | NULL | 20 | 4 | 2001 | 3000 |
    70. | 007876 | ADAMS | CLERK | 7788 | 1987-05-23 00:00:00 | 1100.00 | NULL | 20 | 5 | 3001 | 9999 |
    71. | 007900 | JAMES | CLERK | 7698 | 1981-12-03 00:00:00 | 950.00 | NULL | 30 | 1 | 700 | 1200 |
    72. | 007900 | JAMES | CLERK | 7698 | 1981-12-03 00:00:00 | 950.00 | NULL | 30 | 2 | 1201 | 1400 |
    73. | 007900 | JAMES | CLERK | 7698 | 1981-12-03 00:00:00 | 950.00 | NULL | 30 | 3 | 1401 | 2000 |
    74. | 007900 | JAMES | CLERK | 7698 | 1981-12-03 00:00:00 | 950.00 | NULL | 30 | 4 | 2001 | 3000 |
    75. | 007900 | JAMES | CLERK | 7698 | 1981-12-03 00:00:00 | 950.00 | NULL | 30 | 5 | 3001 | 9999 |
    76. | 007902 | FORD | ANALYST | 7566 | 1981-12-03 00:00:00 | 3000.00 | NULL | 20 | 1 | 700 | 1200 |
    77. | 007902 | FORD | ANALYST | 7566 | 1981-12-03 00:00:00 | 3000.00 | NULL | 20 | 2 | 1201 | 1400 |
    78. | 007902 | FORD | ANALYST | 7566 | 1981-12-03 00:00:00 | 3000.00 | NULL | 20 | 3 | 1401 | 2000 |
    79. | 007902 | FORD | ANALYST | 7566 | 1981-12-03 00:00:00 | 3000.00 | NULL | 20 | 4 | 2001 | 3000 |
    80. | 007902 | FORD | ANALYST | 7566 | 1981-12-03 00:00:00 | 3000.00 | NULL | 20 | 5 | 3001 | 9999 |
    81. | 007934 | MILLER | CLERK | 7782 | 1982-01-23 00:00:00 | 1300.00 | NULL | 10 | 1 | 700 | 1200 |
    82. | 007934 | MILLER | CLERK | 7782 | 1982-01-23 00:00:00 | 1300.00 | NULL | 10 | 2 | 1201 | 1400 |
    83. | 007934 | MILLER | CLERK | 7782 | 1982-01-23 00:00:00 | 1300.00 | NULL | 10 | 3 | 1401 | 2000 |
    84. | 007934 | MILLER | CLERK | 7782 | 1982-01-23 00:00:00 | 1300.00 | NULL | 10 | 4 | 2001 | 3000 |
    85. | 007934 | MILLER | CLERK | 7782 | 1982-01-23 00:00:00 | 1300.00 | NULL | 10 | 5 | 3001 | 9999 |
    86. +--------+--------+-----------+------+---------------------+---------+---------+--------+-------+-------+-------+
    87. 70 rows in set (0.00 sec)
    88. mysql> select ename,sal ,grade from emp,salgrade;
    89. +--------+---------+-------+
    90. | ename | sal | grade |
    91. +--------+---------+-------+
    92. | SMITH | 800.00 | 1 |
    93. | SMITH | 800.00 | 2 |
    94. | SMITH | 800.00 | 3 |
    95. | SMITH | 800.00 | 4 |
    96. | SMITH | 800.00 | 5 |
    97. | ALLEN | 1600.00 | 1 |
    98. | ALLEN | 1600.00 | 2 |
    99. | ALLEN | 1600.00 | 3 |
    100. | ALLEN | 1600.00 | 4 |
    101. | ALLEN | 1600.00 | 5 |
    102. | WARD | 1250.00 | 1 |
    103. | WARD | 1250.00 | 2 |
    104. | WARD | 1250.00 | 3 |
    105. | WARD | 1250.00 | 4 |
    106. | WARD | 1250.00 | 5 |
    107. | JONES | 2975.00 | 1 |
    108. | JONES | 2975.00 | 2 |
    109. | JONES | 2975.00 | 3 |
    110. | JONES | 2975.00 | 4 |
    111. | JONES | 2975.00 | 5 |
    112. | MARTIN | 1250.00 | 1 |
    113. | MARTIN | 1250.00 | 2 |
    114. | MARTIN | 1250.00 | 3 |
    115. | MARTIN | 1250.00 | 4 |
    116. | MARTIN | 1250.00 | 5 |
    117. | BLAKE | 2850.00 | 1 |
    118. | BLAKE | 2850.00 | 2 |
    119. | BLAKE | 2850.00 | 3 |
    120. | BLAKE | 2850.00 | 4 |
    121. | BLAKE | 2850.00 | 5 |
    122. | CLARK | 2450.00 | 1 |
    123. | CLARK | 2450.00 | 2 |
    124. | CLARK | 2450.00 | 3 |
    125. | CLARK | 2450.00 | 4 |
    126. | CLARK | 2450.00 | 5 |
    127. | SCOTT | 3000.00 | 1 |
    128. | SCOTT | 3000.00 | 2 |
    129. | SCOTT | 3000.00 | 3 |
    130. | SCOTT | 3000.00 | 4 |
    131. | SCOTT | 3000.00 | 5 |
    132. | KING | 5000.00 | 1 |
    133. | KING | 5000.00 | 2 |
    134. | KING | 5000.00 | 3 |
    135. | KING | 5000.00 | 4 |
    136. | KING | 5000.00 | 5 |
    137. | TURNER | 1500.00 | 1 |
    138. | TURNER | 1500.00 | 2 |
    139. | TURNER | 1500.00 | 3 |
    140. | TURNER | 1500.00 | 4 |
    141. | TURNER | 1500.00 | 5 |
    142. | ADAMS | 1100.00 | 1 |
    143. | ADAMS | 1100.00 | 2 |
    144. | ADAMS | 1100.00 | 3 |
    145. | ADAMS | 1100.00 | 4 |
    146. | ADAMS | 1100.00 | 5 |
    147. | JAMES | 950.00 | 1 |
    148. | JAMES | 950.00 | 2 |
    149. | JAMES | 950.00 | 3 |
    150. | JAMES | 950.00 | 4 |
    151. | JAMES | 950.00 | 5 |
    152. | FORD | 3000.00 | 1 |
    153. | FORD | 3000.00 | 2 |
    154. | FORD | 3000.00 | 3 |
    155. | FORD | 3000.00 | 4 |
    156. | FORD | 3000.00 | 5 |
    157. | MILLER | 1300.00 | 1 |
    158. | MILLER | 1300.00 | 2 |
    159. | MILLER | 1300.00 | 3 |
    160. | MILLER | 1300.00 | 4 |
    161. | MILLER | 1300.00 | 5 |
    162. +--------+---------+-------+
    163. 70 rows in set (0.00 sec)
    164. mysql> select ename,sal ,grade from emp,salgrade where sal between losal and hisal;
    165. +--------+---------+-------+
    166. | ename | sal | grade |
    167. +--------+---------+-------+
    168. | SMITH | 800.00 | 1 |
    169. | ALLEN | 1600.00 | 3 |
    170. | WARD | 1250.00 | 2 |
    171. | JONES | 2975.00 | 4 |
    172. | MARTIN | 1250.00 | 2 |
    173. | BLAKE | 2850.00 | 4 |
    174. | CLARK | 2450.00 | 4 |
    175. | SCOTT | 3000.00 | 4 |
    176. | KING | 5000.00 | 5 |
    177. | TURNER | 1500.00 | 3 |
    178. | ADAMS | 1100.00 | 1 |
    179. | JAMES | 950.00 | 1 |
    180. | FORD | 3000.00 | 4 |
    181. | MILLER | 1300.00 | 2 |
    182. +--------+---------+-------+
    183. 14 rows in set (0.00 sec)
    184. mysql> select ename,sal ,grade,losal ,hisal from emp,salgrade where sal between losal and hisal;
    185. +--------+---------+-------+-------+-------+
    186. | ename | sal | grade | losal | hisal |
    187. +--------+---------+-------+-------+-------+
    188. | SMITH | 800.00 | 1 | 700 | 1200 |
    189. | ALLEN | 1600.00 | 3 | 1401 | 2000 |
    190. | WARD | 1250.00 | 2 | 1201 | 1400 |
    191. | JONES | 2975.00 | 4 | 2001 | 3000 |
    192. | MARTIN | 1250.00 | 2 | 1201 | 1400 |
    193. | BLAKE | 2850.00 | 4 | 2001 | 3000 |
    194. | CLARK | 2450.00 | 4 | 2001 | 3000 |
    195. | SCOTT | 3000.00 | 4 | 2001 | 3000 |
    196. | KING | 5000.00 | 5 | 3001 | 9999 |
    197. | TURNER | 1500.00 | 3 | 1401 | 2000 |
    198. | ADAMS | 1100.00 | 1 | 700 | 1200 |
    199. | JAMES | 950.00 | 1 | 700 | 1200 |
    200. | FORD | 3000.00 | 4 | 2001 | 3000 |
    201. | MILLER | 1300.00 | 2 | 1201 | 1400 |
    202. +--------+---------+-------+-------+-------+
    203. 14 rows in set (0.00 sec)
    204. mysql>

    这些MySQL命令和查询展示了如何使用empsalgrade表来匹配员工的薪资与相应的薪资等级。通过正确应用条件语句,可以有效地筛选出符合特定薪资范围的员工记录,并将其与相应的薪资等级关联起来。

    直接笛卡尔积查询: 初始查询select *from emp,salgrade;产生了一个笛卡尔积,这意味着emp表中的每一行都与salgrade表中的每一行配对,不考虑任何匹配条件。这通常不是期望的结果,因为它返回了所有可能的行组合,而不是根据实际的薪资等级来筛选员工。

    筛选匹配薪资等级的员工: 通过执行select ename,sal ,grade from emp,salgrade where sal between losal and hisal;,我们可以获得一个更实用的结果集,其中只包括那些其薪资位于salgrade表定义的最低薪资(losal)和最高薪资(hisal)范围内的员工。这种查询方式直观地展示了如何将两个表中的数据根据实际业务逻辑关联起来。

    增加薪资范围信息: 最后的查询select ename,sal ,grade,losal ,hisal from emp,salgrade where sal between losal and hisal;进一步扩展了上一查询,除了返回员工的姓名、薪资和薪资等级外,还包括了确定该薪资等级的最低和最高薪资范围。这提供了更详细的信息,帮助理解为什么特定的员工会被归入特定的薪资等级。

    结尾

    最后,感谢您阅读我的文章,希望这些内容能够对您有所启发和帮助。如果您有任何问题或想要分享您的观点,请随时在评论区留言。

    同时,不要忘记订阅我的博客以获取更多有趣的内容。在未来的文章中,我将继续探讨这个话题的不同方面,为您呈现更多深度和见解。

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

  • 相关阅读:
    Java.lang.Class类 getDeclaredMethod()方法有什么功能呢?
    WMWS在线监测管理系统的工程常用计算工具
    期末复习总结【MySQL】五种约束类型, 主键和外键的使用方式(重点)
    嵌入式C语言中整形溢出问题分析
    阿里P8熬了一个月肝出这份32W字Java面试手册,在Github标星31K+
    js中导入引用外部js
    nginx安装(离线安装,新增--with-http_ssl_module、--with-stream模块,离线升级)
    拼多多不满“国内市场”,将于9月中旬在美国推出跨境电商平台
    基于JAVA民宿网站管理系统计算机毕业设计源码+数据库+lw文档+系统+部署
    Java 基础 进程与线程
  • 原文地址:https://blog.csdn.net/m0_74163972/article/details/136411498