根据匹配订单ID从orderdetails表中获取product_id的计数

时间:2017-06-26 09:24:40

标签: mysql sql

我正在尝试从mysql预期输出中的三个数据表中检索列:

  • 计数(product_id在orderdetails表中的时间)
  • id (来自products表)
  • 名称(来自products表)

订单表

+-----+--------+
| id  | status |
+-----+--------+
| 124 | unpaid |
| 125 | paid   |
| 126 | paid   |
| 127 | paid   |
| 128 | unpaid |
+-----+--------+

orderdetails表

+-----+------------+----------+
| id  | product_id | order_id |
+-----+------------+----------+
| 332 |          1 |      125 |
| 333 |       NULL |      125 |
| 334 |        144 |      125 |
| 335 |        162 |      126 |
| 336 |        144 |      126 |
| 337 |        162 |      127 |
+-----+------------+----------+

产品表

+-----+------------------------------------------------------+
| id  | name                                                 |
+-----+------------------------------------------------------+
|   1 | Chaacoca Argan Oil Daily Moisture Repair Conditioner |
| 144 | Sandalwood 8oz Hand and Body Lotion                  |
| 162 | Mayumi Squalane Skin Oil - 2.17 fl oz                |
+-----+------------------------------------------------------+

预期输出应如下所示:

+------------+----------+-----------------------------------------------------+
| product_id |count_prod|Product_name                                         |
+------------+----------+-----------------------------------------------------+
|          1 |      1   |Chaacoca Argan Oil Daily Moisture Repair Conditioner |
|        144 |      2   |Sandalwood 8oz Hand and Body Lotion                  |
|        162 |      2   |Mayumi Squalane Skin Oil - 2.17 fl oz                |
+------------+----------+-----------------------------------------------------+

任何建议的查询都会有所帮助。

谢谢(提前)!

3 个答案:

答案 0 :(得分:0)

请尝试

select p.id,od.count_prod,p.name
from products as p,
 (select product_id ,count(*) as count_prod from orderdetails group by product_id) as od
where p.id = od.product_id

查询快照: enter image description here

答案 1 :(得分:0)

你可以使用连接,如:

select a.*,count(b.order_id) as total_order,GROUP_CONCAT(c.id) as product_ids,GROUP_CONCAT(c.name) as product_names
from Orders a 
left outer join orderdetails b on a.id = b.order_id
left outer join products c on b.product_id = c.id
group by a.id

答案 2 :(得分:0)

假设所有订单至少有一个详细信息,那么您就不需要orders表。你可以这样做:

select od.order_id, count(*) as num_details,
       group_concat(od.product_id) as product_ids,
       group_concat(p.name) as product_names
from orderdetails od left join
     products p
     on od.product_id = p.id
group by od.order_id;

如果可以在orderdetails中为单个订单复制产品,那么您可以使用distinct来避免重复:

select od.order_id, count(distinct od.product_id) as num_details,
       group_concat(distinct od.product_id) as product_ids,
       group_concat(distinct p.name) as product_names
from orderdetails od left join
     products p
     on od.product_id = p.id
group by od.order_id;

编辑:

根据修订后的问题,您希望按产品计算。这很简单:

select p.id, p.name, count(od.product_id) as num_details
from products p join
     orderdetails od 
     on od.product_id = p.id
group by p.id, p.name;

如果您想要包含没有订单详情的产品,请使用left join代替join