我有个问题,所以我有两张桌子:
订单号:

id   supplier_id    status
1    2              3
2    4              5
3    2              7

礼品订单:
order_id  gift_id  order_price
1         2        4
2         1        5
3         2        6
1         6        1
2         4        9

所以我想得到所有的订单,在表中重复多少次:gift_2_ordertotal price。对于这个例子,我想得到:
order    number_repetitions      total_price
1        2                        5
2        2                        14
3        1                        6

我试过:
SELECT
    ord.estimation_date,
    ord.order_date,
    ord.supplier_id,
    ord.status,
    count.(g2o.order_id),
    sum.(g2o.order_price)
FROM `order` ord
INNER JOIN gift_2_order g2o on ord.id = g2o.order_id

你能帮帮我吗?提前谢谢你。

最佳答案

您必须按ord.id分组并从.中删除count and sum

         SELECT
                ord.id,
                count(g2o.order_id),
                sum(g2o.order_price)
            FROM `order` ord
            INNER JOIN gift_2_order g2o on ord.id = g2o.order_id
         group by ord.id order by ord.id

09-15 12:38