Show result from one table

Nice day, I have these 3 tables ... ie;

customer table

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

      

transaction table

order_num   cust_id   sales_employee
1001        1         1 
1002        2         2

      

Sales_employee table

sales_employee     employee name
1                  john doe
2                  jane doe

      

how can I show employee name in both customer table and transaction table? note how the salesperson sales_employee can change per transaction, it doesn't have to be the same for every customer.

Please, help.

+3


source to share


1 answer


To select customers with seller name

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

      

To select transactions with customer name and seller name (at the time of the transaction)



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

      

This code is for you, you will need to tweak it to match your table and field names.

+1


source







All Articles