简体   繁体   中英

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

I have this MySQL query:

    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 reserved_date product_id quantity order_id
134 2020-07-07 109 1 274
138 2020-07-07 109 1 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.

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:

order_item_id reserved_date product_id SUM(quantity) order_id
134 2020-07-07 109 16 274

I think this is using the total rows which would be available before the 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.

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 109 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). 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. 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:

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.

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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