【问题标题】:SQL where clause two seperate values? [closed]SQL where 子句有两个单独的值? [关闭]
【发布时间】:2022-09-27 16:18:04
【问题描述】:

所以对于一个任务,我需要查找支持来自加拿大的客户的员工的经理来自chinook data base

现在我有以下代码:

select distinct employee.LastName, employee.EmployeeId, employee.ReportsTo
from customer,
     employee
where customer.Country = \'Canada\'
  and customer.SupportRepId = employee.EmployeeId;

所以我得到以下结果

所以我需要获取EmployeeId为2的总经理的姓名,但我不知道如何将其放入一个查询中。

这是所有员工的概述:

这是所有客户的概述:

  • 您可能会惊讶有多少问题被忽略了,因为它们不是自包含的,并期望我们追踪链接。继续。
  • 我可以看到customer 桌子吗?
  • 不要在 where 语句中使用旧的连接表变体。使用 join 关键字
  • @Jens 同意
  • 还将示例数据显示为预期结果以及作为文本而不是链接获得的结果

标签: mysql sql


【解决方案1】:

您必须两次加入员工表:

select distinct employee.LastName, employee.EmployeeId, manager.Lastname
from 
 customer 
 join     employee as employee on customer.SupportRepId = employee.EmployeeId
 join employee as manager on employee.ReportsTo = manager.employeeId
where customer.Country = 'Canada'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-16
    • 2019-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-03
    相关资源
    最近更新 更多