Skip to content
Advertisement

MySQL – summing up products from several orders

I want to sum up the number of items purchased from several orders. I have this query, however it shows me each order on a separate row. I want have it all added up in one row. This is woocommerce database.

Example output:

and this is how it should be:

Advertisement

Answer

You want the aggregation string over all those items, so the aggregation must be the main query. Look up the desired order IDs with IN or EXISTS.

Maybe you can remove the join on wp_postmeta in the subquery. That depends on which table the column post_type resides in.

(I hope I got everything right. I just took your query and re-wrote it. I don’t know the data model and I don’t know if a post ID can really equal an order ID, but this is how you use them in your query.)

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement