6表连接SQL查询难度

时间:2016-06-29 12:54:26

标签: mysql

我有以下6个表(简化发布):

订单:

    +----------+--------------+-------------------+--------------+
    | Order_ID | Sales_Area_ID| Project_Status_ID | Active       | 
    +----------+--------------+-------------------+--------------+
    |  1       | 3            | 2                 | 1            |
    |  2       | 2            | 2                 | 1            |
    |  3       | 3            | 3                 | 1            |
    |  4       | 2            | 1                 | 1            |
    +----------+--------------+-------------------+--------------+

Order_Release:

    +------------------+---------+--------------------+--------------------+
    | Order_Release_ID | Order_ID| Sales_Release_Date | Currency_Rate      |  
    +------------------+---------+--------------------+--------------------+
    |  1               | 1       | 2016/06/25         | 1                  |
    |  2               | 2       | 2016/02/19         | 1                  |
    |  3               | 1       | 2016/06/23         | 1.2                |
    |  4               | 2       | 2016/06/22         | 1                  |
    +------------------+---------+--------------------+--------------------+

Rooflight_Request(产品类型A):

    +----------------------+---------+--------+---------+--------+----------------+
    | Rooflight_Request_ID | Order_ID| Price  | Discount| Qty    | Record_Active  |  
    +----------------------+---------+--------+---------+--------+----------------+
    |  1                   | 1       | 1200   | 50      | 2      | 1              |
    |  2                   | 2       | 1178   | 0       | 1      | 1              |
    |  3                   | 1       | 500    | 20      | 12     | 1              |
    |  4                   | 2       | 3521   | 0       | 1      | 0              |
    +----------------------+---------+--------+---------+--------+----------------+

Rooflight_Request(产品类型B):

    +----------------------------+---------+--------+---------+--------+----------------+
    | Rooflight_Extra_Request_ID | Order_ID| Price  | Discount| Qty    | Record_Active  |  
    +----------------------------+---------+--------+---------+--------+----------------+
    |  1                         | 1       | 200    | 50      | 1      | 0              |
    |  2                         | 2       | 178    | 0       | 3      | 1              |
    |  3                         | 1       | 50     | 20      | 5      | 1              |
    |  4                         | 3       | 121    | 0       | 10     | 1              |
    +----------------------------+---------+--------+---------+--------+----------------+   

Sales_Area:

    +---------------+----------------+--------------+
    | Sales_Area_ID | Sales_Area_Name| Active       | 
    +---------------+----------------+--------------+
    |  1            | East           | 0            |
    |  2            | Lon 1          | 1            |
    |  3            | Lon 2          | 1            |
    |  4            | USA            | 1            |
    +---------------+----------------+--------------+

Project_Status:

    +-------------------+----------------+
    | Project_Status_ID | Project_Status |
    +-------------------+----------------+
    |  1                | Quote          |
    |  2                | Released       |
    |  3                | Invoiced       |
    |  4                | Released_PM    |
    |  5                | Credit_Note    |
    +-------------------+----------------+

我正在尝试编写一个查询,该查询返回按销售区域行分组的列中订单商品的总价值。因此,即使项目总数为零,我也希望返回所有销售区域 我正在尝试使用以下规则:在订单发布日期窗口内,仅将活动项目添加到总计。到目前为止,我有这个问题:

    SELECT salesArea.Area_Name,

            (SELECT SUM(((rr.Price - (rr.Price * (rr.Discount /100))) / orders.Currency_Rate) * rr.Quantity) 
                FROM Rooflight_Request AS rr
                WHERE rr.Order_ID = orders.Order_ID
                AND rr.Record_Active = 1 
                AND rr.Alternative_Option <> 1
            ) AS Rooflights_Total,

            (SELECT SUM(((rx.Price - (rx.Price * (rx.Discount /100))) / orders.Currency_Rate) * rx.Quantity)
                FROM Rooflight_Extra_Request AS rx
                WHERE rx.Order_ID = orders.Order_ID
                AND rx.Record_Active = 1
                AND rx.Alternative_Option <> 1
            ) AS Extras_Total

            FROM Orders AS orders
                LEFT JOIN Order_Release AS orderRelease ON
                    orders.Order_ID = orderRelease.Order_ID
                LEFT JOIN Project_Status AS projectStatus ON 
                    orders.Project_Status_ID = projectStatus.Project_Status_ID 
                    AND projectStatus.Record_Active = 1 
                LEFT JOIN Sales_Area AS salesArea ON 
                    orders.Sales_Area_ID = salesArea.Sales_Area_ID 
                    AND salesArea.Record_Active = 1

            WHERE orderRelease.Sales_Release_Date >= 2016/06/22
            AND orderRelease.Sales_Release_Date <= 2016/06/29
            AND (projectStatus.Project_Status = 'Invoiced' 
                OR projectStatus.Project_Status = 'Released' 
                OR projectStatus.Project_Status = 'Released PM')
            GROUP BY orders.Order_ID

查询返回零结果。如果我删除了sales_Release_Date where子句,我得到了很多结果但是并不完全是每个的订单项只从每个中获取第一个结果集。 我已经尝试了很多不同的安排,但似乎无法解决这个问题。 Cany有人帮忙吗?谢谢

1 个答案:

答案 0 :(得分:0)

如果sales_Release_Date是一个字符串,则在引号中包含条件..

orderRelease.Sales_Release_Date >= '2016/06/22'

如果是日期类型,则需要包含日期函数.. str_to_date