我有这张桌子
订单
制品
客户
和SQL Join Table
$sql="SELECT customer_name, article_name, orders_id FROM orders
LEFT JOIN articles ON order_article_id = articles_id
LEFT JOIN customers on order_customer_id = customers_id";
从这个查询我需要获得AMOUNT。 数量是示例
USER:MICHAEL
AMOUNT:订单ID = order_article_id + order_qty;
是否可以在MySQL中执行此操作,或者我需要一些额外的PHP代码?
答案 0 :(得分:0)
您可以使用GROUP BY查询和SUM()聚合函数在MySQL中执行此操作:
SELECT
c.customer_name,
SUM(a.article_price*a.article_quantity) AS amount
FROM
orders o LEFT JOIN articles a
ON o.order_article_id = a.articles_id
LEFT JOIN customers c
ON o.order_customer_id = c.customers_id
GROUP BY
c.customers_id,
c.customer_name
您可能希望获得每个订单的AMOUNT,然后您还需要为order_id分组:
GROUP BY
o.orders_id,
c.customers_id,
c.customer_name