Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7675903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:04:32+00:00 2026-05-31T17:04:32+00:00

I am trying to list product variations with their quantities ordered, BUT ALSO show

  • 0

I am trying to list product variations with their quantities ordered, BUT ALSO show the product variations where there is no quantity ordered. So I thought it would be as simple as selecting the products and doing a left join on the orders of each product where the order is a current revision.

So I expected like this order of operations:

SELECT p.product_id, SUM(po.quantity) 
FROM `products` p 
LEFT JOIN `product_orders` po ON p.product_id=po.product_id 
LEFT JOIN `orders` o ON o.order_id=po.order_id AND o.is_current='1'

but that is getting also the quantities where the is_current is not 1
Then I thought, okay, I can just do an inner join after the left join instead like this:

SELECT p.product_id, SUM(po.quantity) 
FROM `products` p 
LEFT JOIN `product_orders` po ON p.product_id=po.product_id 
INNER JOIN `orders` o ON o.order_id=po.order_id AND o.is_current='1'

but then the products which have not been ordered yet are not being listed. I expected them to to show up as SUM(quantity) being NULL.
Can anyone see where my logic has gone wrong?
Thanks!
Scott

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T17:04:33+00:00Added an answer on May 31, 2026 at 5:04 pm

    If the only product orders that count are those where it is current then you need to find that subset before you do a left join to it. Otherwise if you do a left join you either get all or only those ordered as you’ve discovered.

    So something like the following should work:

    select p.productid, sum(po.quantity)  
    from products p  
    left outer join (select po.productid, po.quantity  
                           from productorders po  
                           inner join orders o on o.orderid = po.orderid and o.iscurrent = 1) po on po.productid = p.productid  
    group by p.productid
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables containing product information, and product variations. I’m trying to list
I am trying to show the popular product list using ajax in magento on
I am trying to list all the controllers and their actions with specific attributes
With rails version 2.3.8 I'm trying to get HTML output like: <a href=/product/list data-role=button>Product
I have a simple JQuery Product list. When a user click an item, it
I am trying to create wish lists. There are user and product models. A
I am trying to generate a list of all possible product option/value combinations for
I'm trying to work with mod_rewrite without particular problems but this: RewriteRule ^(product/)([^/\<\>].+?)(/)([^/\<\>].+?)(/edit)/?$ php/prd_edit.php
I ma trying to create 2 filters for a product list page, one for
I'm trying to get a list of IDs (a la find('list') ), but can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.