显示一个表的结果

时间:2013-02-13 03:01:34

标签: sql

好日子,我有这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如何根据每笔交易进行更改,每个客户不一定必须相同。

请帮忙。

1 个答案:

答案 0 :(得分: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

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