简体   繁体   English

使用 INNER JOIN 和 distinct/group by 时数量列的总和

[英]SUM of a quantity column when using INNER JOIN and distinct/group by

I have this MySQL query:我有这个 MySQL 查询:

    SELECT DISTINCT( rentals.order_item_id ), reserved_date, product_id, quantity, order_id FROM `test_rentals` as rentals
    INNER JOIN `test_orderitemmeta` as orderitemmeta
    WHERE `reserved_date` = '2020-07-07'
    AND `product_id` = 109
    AND orderitemmeta.meta_key = 'returned'
    AND orderitemmeta.meta_value = 'yes'

This returns:这将返回:

order_item_id order_item_id reserved_date保留日期 product_id产品编号 quantity数量 order_id order_id
134 134 2020-07-07 2020-07-07 109 109 1 1个 274 274
138 138 2020-07-07 2020-07-07 109 109 1 1个 276 276

The test_orderitemmeta table contains a unique meta_id , order_item_id , meta_key and meta_value , there can be multiple rows with the same order_item_id, the rentals table only has the order_item_id column to join these with. test_orderitemmeta表包含一个唯一的meta_idorder_item_idmeta_keymeta_value ,可以有多行具有相同的 order_item_id , rentals 表只有order_item_id列来加入这些。

I want the return to be a single row showing the reserved_date, product_id and the total quantity, I thought I could do this by using SUM( quantity ) but this gives the result:我希望返回单行显示保留日期、产品 ID 和总数量,我想我可以通过使用SUM( quantity )来做到这一点,但这给出了结果:

order_item_id order_item_id reserved_date保留日期 product_id产品编号 SUM(quantity) SUM(数量) order_id order_id
134 134 2020-07-07 2020-07-07 109 109 16 16 274 274

I think this is using the total rows which would be available before the DISTINCT ?我认为这是在使用DISTINCT之前可用的总行数?

I've also considered removing the DISTINCT( rentals.order_item_id ) and using a GROUP BY rentals.order_item_id on the end of the query but this does not give the result I am looking for either.我还考虑过删除DISTINCT( rentals.order_item_id )并在查询末尾使用GROUP BY rentals.order_item_id但这也没有给出我正在寻找的结果。

How can I get the result to be a single row with the total quantity like this:我怎样才能得到这样的总数量的单行结果:

reserved_date保留日期 product_id产品编号 quantity数量
2020-07-07 2020-07-07 109 109 2 2个

I think this will get you the result you need:我认为这将为您提供所需的结果:

SELECT reserved_date, product_id, SUM(quantity) as quantity
FROM `test_rentals` as rentals, `test_orderitemmeta` as orderitemmeta
WHERE `reserved_date` = '2020-07-07'
    AND `product_id` = 109
    AND orderitemmeta.meta_key = 'returned'
    AND orderitemmeta.meta_value = 'yes'
GROUP BY reserved_date, product_id

DISTINCT is not a function, it is a modifier of the SELECT clause and it applies to the entire row (it does not make sense otherwise). DISTINCT不是 function,它是SELECT子句的修饰符,适用于整行(否则没有意义)。 It does not help here.它在这里没有帮助。

The JOIN without a condition ( ON... ) produces all combinations of rows from the first table with rows from the second table.没有条件的JOIN ( ON... ) 生成第一个表中的行与第二个表中的行的所有组合。 You need a condition to combine only the related rows from the two tables (ie the metadata rows that belong to the desired rental).您需要一个条件来仅组合两个表中的相关行(即属于所需租金的元数据行)。

Assuming that the table test_orderitemmeta contains a column named rental_id that links the metadata record to the rental record that owns the metadata, a query that produces the result you expect looks like:假设表test_orderitemmeta包含一个名为rental_id的列,它将元数据记录链接到拥有元数据的租赁记录,生成您期望的结果的查询如下所示:

SELECT r.reserved_date, r.product_id, SUM(r.quantity) AS quantity
FROM test_rentals AS r
INNER JOIN test_orderitemmeta AS m ON r.rental_id = m.rental_id
WHERE r.reserved_date = '2020-07-07'
  AND r.product_id = 109
  AND m.meta_key = 'returned'
  AND m.meta_value = 'yes'
GROUP BY r.product_id, r.reserved_date 

Replace rental_id with the correct column name from your tables.rental_id替换为表中的正确列名。

You will try this code get the result for you need:您将尝试使用此代码获取所需的结果:

    SELECT rentals.*, orderitemmeta.reserved_date, orderitemmeta.product_id, SUM(orderitemmeta.quantity) as quantity
    FROM `test_rentals` as rentals JOIN test_orderitemmeta as orderitemmeta on orderitemmeta.product_id = rentals.order_item_id 
WHERE orderitemmeta.reserved_date = '2020-07-07'
        AND orderitemmeta.product_id = 109
        AND orderitemmeta.meta_key = 'returned'
        AND orderitemmeta.meta_value = 'yes'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM