取回每位员工2018年销售的产品和服务的总金额

时间:2018-08-22 19:03:37

标签: mysql sql

我希望带回一个结果,以显示每个员工的销售额。 到目前为止,我已经创建了代码以仅显示如下所示的产品。

SELECT 
    Booking.*, 
    Employee_Details.* 
FROM 
    Employee_Details 
INNER JOIN
    Booking ON Booking.Employee_ID_FK = Employee_Details.Employee_ID
WHERE
    Booking_Date LIKE '%2018%' 
ORDER BY 
    Employee_ID_FK

这行得通,但是我也想包括这些服务,因此尝试添加它,但似乎不起作用。

这是我尝试过的:

SELECT 
    Booking.*, 
    Employee_Details.*, 
    Products.*, 
    Services.Charge_Per_Service
FROM 
    Employee_Details 
    INNER JOIN Booking ON Booking.Employee_ID_FK = Employee_Details.Employee_ID 
    INNER JOIN Products ON Booking.Product_Name_FK = Products.Product_Name
    INNER JOIN Services ON Booking.Service_Name_FK = Services.Service_Name 
-- WHERE Booking_Date LIKE '%2018%' ORDER BY Employee_ID_FK

但是,这没有用。我试着取出“哪里”,但这也不起作用。谁能解释需要做什么?另外,如果我们可以指定年份(2018),就像上面的代码中所评论的那样,那太好了!

请参阅以下有关我使用的表的链接:

Please see here for my Booking Table

Please see here for my Employee Table

Please see here for my Product Table

Please see here for my Services Table

1 个答案:

答案 0 :(得分:0)

您可以尝试基于OUTER JOINProducts中的Booking表使用Services,因为您使用inner join将从示例数据中考虑所有表值条件值,将没有行匹配。

有一种方法year可以从日期类型值中获取年份数字。

SELECT 
    Booking.*, 
    Employee_Details.*, 
    Products.*, 
    Services.Charge_Per_Service
FROM 
    Booking
    INNER JOIN Employee_Details ON Booking.Employee_ID_FK = Employee_Details.Employee_ID 
    LEFT JOIN Products ON Booking.Product_Name_FK = Products.Product_Name
    LEFT JOIN Services ON Booking.Service_Name_FK = Services.Service_Name
WHERE yaer(Booking_Date) = 2018 
ORDER BY Employee_ID_FK