简体   繁体   English

在PHP中与内连接求和

[英]Sum with inner join in PHP

I'm working with PHP and MySQL, and I need to SUM the total amount of products joining 3 tables: 我正在使用PHP和MySQL,我需要汇总连接3个表的产品总数:

order_products: (There are multiple order products with the same name but different amounts in the table) order_products :(有多个订单产品,名称相同但表中的金额不同)

order_id        (int)
product_name    (varchar)
product_amount  (int)

orders: 命令:

order_id       (int)
order_date     (varchar)
order_status   (varchar)

supplier: 供应商:

product_name    (varchar)
product_amount  (int)

So, I want to show how many products I sold and status is shipped and how many I ordered from the supplier in one single row. 因此,我想展示我销售的产品数量和状态,以及我从一个供应商订购的产品数量。 Any of two examples below will help me to achieve my goal. 以下两个例子中的任何一个都将帮助我实现我的目标。

Like: 喜欢:

Product Name     (sum order_products)      (sum supplier)  Order status

first product           300                  2500          Shipped_Only
second product          50                   400           Shipped_Only
third product           10                   600           Shipped_Only


Product Name     (sum order_products)      (sum supplier)  Order status

first product           2200                 2500          Not_Shipped
second product          400                  400           Not_Shipped
third product           590                  600           Not_Shipped

Are there any examples or other help that I can get to do this? 我可以做任何示例或其他帮助吗?

Edit: Sample Data goes like this 编辑:示例数据是这样的

order_products: order_products:

order_id       product_name     product_amount

255               product 1         200
256               product 1         100
257               product 2         50
258               product 3         10

orders: order_id order_date order_status 订单: order_id order_date order_status

255               09.05.2018         Shipped
256               09.05.2018         Shipped
257               10.05.2018         Not_Shipped
258               10.05.2018         Not_Shipped

supplier: product_name product_amount 供应商: product_name product_amount

product 1         2500        
product 2         400   
product 3         600    

You should use a join on the aggregated subselect. 您应该在聚合子选择上使用联接。

SELECT t1.product_name, t1.sum_order_products, t2.supplier_sum, t1.order_status 
FROM (
    SELECT op.product_name, SUM(op.product_amount) sum_order_products, o.order_status
    FROM order_products op
    INNER JOIN orders o ON op.order_id = o.order_id 
    WHERE o.order_status = 'Shipped'
    GROUP BY op.product_name, o.order_status
) t1 
LEFT JOIN (
    SELECT s.product_name, SUM(s.product_amount) supplier_sum
    FROM supplier s 
    GROUP BY s.product_name
) t2 ON t1.product_name = t2.product_name 
ORDER BY t1.order_status, t1.product_name

From What I understand, I think this is what you want, please give more clarity if this is not what you are expecting. 根据我的理解,我认为这就是你想要的,如果这不是你所期望的,请更清楚。

You will need to use GROUP BY clause and them you will have to use count() function to count the number of rows for the results coming from Group By Clause. 您将需要使用GROUP BY子句,它们必须使用count()函数来计算来自Group By Clause的结果的行数。 I am writing an example of how to use a group by clause, you will need to modify the query as per your need. 我正在编写一个如何使用group by子句的示例,您需要根据需要修改查询。

SELECT
 order_products.product_name,
 count(*) as Total_Orders,
 MAX(supplier.product_amount) as Supplier_Amt,
 orders.order_status
FROM supplier
 INNER JOIN order_products ON supplier.product_name = order_products.product_name
 INNER JOIN orders ON orders.order_id = order_products.order_id
WHERE orders.order_status = 'Not_Shipped'
GROUP BY order_products.product_name, orders.order_status;

You will need to queries, you can write the other one, just replace WHERE orders.order_status = 'Not_Shipped' with WHERE orders.order_status = 'Shipped' Also if you want all in a single query, simply remove the where clause. 您将需要查询,您可以编写另一个查询,只需将WHERE orders.order_status = 'Not_Shipped'替换为WHERE orders.order_status = 'Not_Shipped' WHERE orders.order_status = 'Shipped'此外,如果您想在一个查询中完成所有操作,只需删除where子句即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM