gpt4 book ai didi

sql - 显示一张表的结果

转载 作者:行者123 更新时间:2023-12-04 13:53:53 25 4
gpt4 key购买 nike

美好的一天,我有这 3 张 table ......即;

客户表

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 如何在每笔交易中发生变化,它不一定必须与每个客户相同。

请帮忙。

最佳答案

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

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

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

关于sql - 显示一张表的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14845715/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com