与三个表的Mysql连接都没有像预期的那样工作

时间:2018-05-09 11:33:13

标签: mysql sql join

我有三个不同的表,一个是带有请求数量和价格的products_request表,第二个是带有订单ID详细信息的products_header_response,第三个是products_response表作为确认数量和价格。现在我想查询总请求数量乘以价格(指以美元计算的所需产品总数和确认数量乘以价格(以美元计算的已确认产品总数)。但是我收到了一些订单的错误金额和一些订单的正确金额。

For example correct scenario is total requested quantity values is $200 then confirmed quantity values is also $200 (if we confirm all requested products)
Wrong scenario is requested quantity value is $200 and confirmed quantity values is more than requested quantity value like some $300. 

以下是我加入3个表的查询。

SELECT resp.id,resp.order_number,resp.orderdate,
ROUND(SUM(req.`requested_quantity` * req.`net_price`),2) AS Req_Order_Amnt,
ROUND((SUM(items_resp.dispatch_quantity * items_resp.`net_price`)+ SUM(items_resp.backorder_quantity * items_resp.`net_price`)),2) 
AS Tot_Confirmed_Amnt,
req.edi_order_id,req.requested_quantity
FROM products_header_response resp
JOIN products_request req 
ON req.`edi_order_id` = resp.`order_number`
JOIN products_response items_resp 
ON resp.id= items_resp.`edi_order_response_id` 
WHERE DATE(resp.orderdate) BETWEEN '20180411' AND '20180411'  AND (items_resp.`dispatch_quantity`!='' OR items_resp.`backorder_quantity`!='')
GROUP BY items_resp.`edi_order_response_id`

在products_response表中,确认的数量将分两列发送,即dispatch_quantity和backorder_quantity。如果延期交货数量在那里,那么在backorder_quantity列中,如果有调度数量,那么在dispatch_quantity列中,或者如果两个数量都存在,那么在两个列中将发送数量。

For example requested quantity 100, then if dispatch quantity is 50 and backorder quantity is 20  then 50, 20 will be sent in respective columns and 30 will be sent as rejected quantity.

以下两个表的加入工作与预期的一样,但从此加入我只能获得确认的数量金额。

SELECT resp.id,resp.order_number,resp.orderdate,
ROUND((SUM(items_resp.dispatch_quantity * items_resp.`net_price`) + SUM(items_resp.backorder_quantity * items_resp.`net_price`)),2) 
AS Tot_Confirmed_Amnt
FROM product_header_response resp
JOIN products_response items_resp 
ON resp.id= items_resp.`edi_order_response_id` 
WHERE DATE(resp.orderdate) BETWEEN '20180411' AND '20180411'  
AND (items_resp.`dispatch_quantity`!='' OR items_resp.`backorder_quantity`!='')
GROUP BY items_resp.`edi_order_response_id`

请纠正我在连接错误的地方。任何帮助将不胜感激。

products_respose table
id| order_number
3456 ABC123

Products Request table 
edi_order_id| requested_quantity|net_price|product_id
ABC123           2                26.44    pr123 
ABC123           2                22.29    pr234
ABC123           1                12.34    pr321 
ABC123           2                20.05    pr345
ABC123           1                18.26    pr546
ABC123           2                26.48    pr456
ABC123           2                26.44    pr567

产品响应表

dispatch_quantity|backorder_quantity|net_price | product_id | edi_order_resp_id
-                   -                 26.44         pr123         3456
-                   -                 22.29         pr234         3456
-                   -                 12.34         pr321         3456
2                   -                 20.05         pr345         3456
-                   -                 18.26         pr546         3456
2                   -                 26.48         pr456         3456
-                   -                 26.44         pr567         3456

预期结果应为

Order_id | Tot_req_Qty_amount |Tot_confirmed_Qty_amount| Requested_products
ABC123     274.00               93.96                      12

但是我从3个连接中得到了以下错误的结果

Order_id | Tot_req_Qty_amount |Tot_confirmed_Qty_amount| Requested_products
    ABC123     548.00               651.42                      24

0 个答案:

没有答案