【问题标题】:Join 3 tables with where clause without common table用where子句连接3个表,没有公用表
【发布时间】:2021-03-12 02:24:09
【问题描述】:

我有 3 张表,员工、销售、部门

员工表

------------------------------
  AssignemtID | EmployeeEmail
-------------------------------
      1       | abc.@cam.com
-------------------------------
      2       | bdc@cam.com
-----------------------------
      3       | dgs@cam.com
-----------------------------
      4       | tyr@cam.com
------------------------------

销售表

Sales | EmployeeEmail     | Emp ID
------------------------------------
24333  | abc.@cam.com  |  34
------------------------------------
46373 | bdc@cam.com    | 24
-----------------------------------
32212 | dgs@cam.com    | 78
-----------------------------------

部门表

Department | Sales       |Emp ID
-------------------------------
AS         | 24333       |34
-------------------------------
we         | 46373       |24
-----------------------------
de         | 32212       |78
------------------------------

我想查看在 Employee 表中但不在 Department 表中的所有员工。所有 3 个表中的数据都很大

【问题讨论】:

  • Sales 表和这个有什么关系?
  • 为什么Employee 表没有Emp ID 列?
  • 这些表没有任何意义。为什么Sales 表有邮件,但没有销售信息?
  • 欢迎来到 Stack Overflow! StackOverflow 不是免费的编码服务。你应该try to solve the problem first。请更新您的问题以在minimal reproducible example 中显示您已经尝试过的内容。如需更多信息,请参阅How to Ask,并拨打tour :)
  • 员工表和销售可以在Employeeemail上连接。销售图上的销售和部门表,但由于没有公共列,无法将员工表连接到部门表

标签: mysql


【解决方案1】:

您可以将Employee 连接到Department,方法是通过Sales 加入它。

SELECT *
FROM Employee
WHERE EmployeeEmail NOT IN (
    SELECT EmployeeEmail
    FROM Sales AS s
    JOIN Department AS d ON s.Sales = d.Sales
)

SELECT e.*
FROM Employee AS e
LEFT JOIN Sales AS s ON s.EmployeeEmail = e.EmployeeEmail
LEFT JOIN Department AS d ON s.Sales = d.Sales
WHERE d.Department IS NULL

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-27
    • 1970-01-01
    • 1970-01-01
    • 2019-08-18
    • 2019-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多