MYSQL:获取每个订单中每个产品的数量

时间:2015-08-07 10:01:03

标签: mysql

我有3个表,orderproductsorders_products,我试图了解每个订单中每个产品的购买量。是否可以在一个查询中获取所有信息?

我当前的查询似乎不起作用,而product_id则完全相同。

SELECT orders.order_id, orders.username, COUNT( DISTINCT op.product_id ) product_id, SUM( op.quantity ) quantity
FROM  `orders` 
JOIN orders_products op ON op.order_id = orders.order_id
GROUP BY product_id, orders.order_id ORDER BY order_id, product_id

结果是:

_______________________________________________
| order_id | username | product_id | quantity |
-----------------------------------------------
|        1 | bill     |          1 |        3 |
-----------------------------------------------
|        1 | bill     |          1 |        2 |
-----------------------------------------------
|        1 | bill     |          1 |        5 |
-----------------------------------------------
|        2 | sally    |          1 |        2 |
-----------------------------------------------
|        3 | jeff     |          1 |        6 |
-----------------------------------------------
|        3 | jeff     |          1 |        7 |
-----------------------------------------------

您可以在product_id列中看到上述问题,该列始终设置为1

我试图得到这样的东西:

_______________________________________________
| order_id | username | product_id | quantity |
-----------------------------------------------
|        1 | bill     |          1 |        5 |
-----------------------------------------------
|        1 | bill     |          2 |        3 |
-----------------------------------------------
|        1 | bill     |          3 |        2 |
-----------------------------------------------
|        2 | sally    |          1 |        2 |
-----------------------------------------------
|        3 | jeff     |          1 |        6 |
-----------------------------------------------
|        3 | jeff     |          2 |        7 |
-----------------------------------------------

我的桌子:

-- Orders
_______________________
| order_id | username |
-----------------------
|        1 | bill     |
-----------------------
|        2 | sally    |
-----------------------
|        3 | jeff     |
-----------------------

-- Products
___________________
|    id | product |
-------------------
|     1 | Table   |
-------------------
|     2 | Chair   |
-------------------
|     3 | Mouse   |
-------------------

-- Order Products
___________________________________________
|   id | order_id | product_id | quantity |
-------------------------------------------
|    1 |        1 |          1 |        5 |
-------------------------------------------
|    2 |        1 |          2 |        3 |
-------------------------------------------
|    3 |        1 |          3 |        2 |
-------------------------------------------
|    4 |        2 |          1 |        2 |
-------------------------------------------
|    5 |        3 |          1 |        6 |
-------------------------------------------
|    6 |        3 |          2 |        7 |
-------------------------------------------

我很感激帮助。我真的遇到了这个问题。

2 个答案:

答案 0 :(得分:1)

我认为您想要的查询是:

SELECT o.order_id, o.username, op.product_id, SUM( op.quantity ) as quantity
FROM  `orders` o JOIN 
      orders_products op
      ON op.order_id = o.order_id
GROUP BY op.product_id, o.order_id
ORDER BY o.order_id, op.product_id;

通常,您确实需要GROUP BYSELECT中的所有列。但(通常)它们不应该是聚合函数的参数,例如COUNT()

答案 1 :(得分:0)

  

您可以在product_id列中看到上述问题   设为1

我认为您在这里有点困惑,这是Order Productsproduct_id列中的数据,但在您的查询中,您要获取的是数据COUNT( DISTINCT op.product_id ) product_id的数量计数为1,结果显示相同。