3

美好的一天,我有这 3 张桌子……即;

客户表

cust_id cust_name sales_employee
1       abc       1
2       cde       1
3       efg       2

事务表

order_num   cust_id   sales_employee
1001        1         1 
1002        2         2

sales_employee 表

sales_employee     employee name
1                  john doe
2                  jane doe

如何在客户表和交易表上显示员工姓名?请注意 sales_employee 可以如何在每笔交易中发生变化,不一定每个客户都必须相同。

请帮忙。

4

1 回答 1

1

选择具有销售人员姓名的客户

select 
  C.*, E.employee_name
from
  Customers as C
  inner join Sales_Employees as E on E.sales_employee = C.sales_employee

选择具有客户姓名和销售人员姓名的交易(在交易的时间点)

select 
  T.*, 
  E.employee_name as Trans_employee, 
  C.cust_name,
  EC.employee_name as Cust_employee
from
  Transactions as T
  inner join Sales_Employees as E on E.sales_employee = T.sales_employee
  inner join Customers as C on C.cust_id= T.cust_id
  inner join Sales_Employees as EC on EC.sales_employee = C.sales_employee

此代码旨在指导您,您需要对其进行调整以匹配您的表和字段名称。

于 2013-02-13T03:06:25.510 回答