0

所以我有这样的查询:

SELECT * FROM my_db.my_order where product_id = 395 order by id desc;

这个表的输出是这样的

+----------+----------+----------+---------------------+------------+--------+
| order_id | buyer_Id | quantity |      createdAt      | product_id | status |
+----------+----------+----------+---------------------+------------+--------+
|     6232 |    89450 |        1 | 2020-05-06 17:44:41 |        395 |      1 |
|     6232 |    89450 |        1 | 2020-05-06 17:44:41 |        395 |      1 |
|     6232 |    23048 |        2 | 2020-05-06 17:44:41 |        395 |      1 |
|     6232 |    89464 |        1 | 2020-05-06 17:44:40 |        395 |      1 |
|     6232 |    89463 |        1 | 2020-05-06 17:44:40 |        395 |      1 |
|     6232 |    89463 |        2 | 2020-05-06 17:43:25 |        395 |      0 |
|     6232 |    89464 |        2 | 2020-05-06 17:43:19 |        395 |      0 |
+----------+----------+----------+---------------------+------------+--------+

所以我想计算状态为= 1的这个product_id的总数量,所以我做了这个查询

SELECT 
    SUM(grouped_my_order_tbl.quantity)
FROM
    (
        SELECT 
            mo.order_seller_id AS order_seller_id,
            mo.quantity AS quantity,
            mo.status AS status,
            mo.product_id AS product_id
        FROM my_order mo
    ) grouped_my_order_tbl
WHERE
    grouped_my_order_tbl.product_id = 395
    AND grouped_my_order_tbl.order_seller_id = 6232
    AND grouped_my_order_tbl.status = 1;

并且计数的输出是 6 而不是 5

我错在哪里?

预期结果 :sum = 5

更新:确切的总和应该是 5 因为有重复的buyer_id 和 createdAt 所以系统读取了两次数据,而不是只有 1 条记录而不是 2 条记录

4

0 回答 0