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 8081057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:36:27+00:00 2026-06-05T16:36:27+00:00

I am new to derived queries with MySQL and am having trouble with two

  • 0

I am new to derived queries with MySQL and am having trouble with two things.

1) How do I reference the t1.dCount so that it counts the number of xcart_images_D.productId for each optionId?

2) How can I streamline the derived SELECT of this query? (Currently this query takes 45 seconds to run)

 SELECT xp.productid, xp.product, xc.classid, xco.optionid, xco.option_name, xiW.id, xiW.image_path, t1.dCount
 FROM xcart_products xp
 INNER JOIN xcart_variants xv ON xp.productid = xv.productid
 INNER JOIN xcart_variant_items xvi ON xv.variantid = xvi.variantid
 INNER JOIN xcart_class_options xco ON xvi.optionid = xco.optionid
 INNER JOIN xcart_classes xc ON xco.classid = xc.classid AND xc.class = 'COLOR'
 LEFT JOIN xcart_images_W xiW ON xiW.id = xvi.variantid
 LEFT JOIN (
 SELECT COUNT(xiD.optionid) as dCount 
    FROM xcart_products xp2
    INNER JOIN xcart_classes xc2 ON xp2.productid = xc2.productid AND xc2.class = 'Color'
    INNER JOIN xcart_class_options xco2 ON xc2.classid = xco2.classid
    LEFT JOIN xcart_images_D xiD ON xiD.optionid = xco2.optionid
 ) as t1 ON xiW.id = xvi.variantid
 GROUP BY xco.optionid
 ORDER by xp.product DESC

Here is a working version of the count but without the derived select –

SELECT xp.productid, xp.product, xc.classid, xco.optionid, xco.option_name, xiD.image_path, xiD.path_on_server, count(xiD.optionid) as cnt
    FROM xcart_products xp
    INNER JOIN xcart_classes xc ON xp.productid = xc.productid AND xc.class = 'Color'
    INNER JOIN xcart_class_options xco ON xc.classid = xco.classid
    LEFT JOIN xcart_images_D xiD ON xiD.optionid = xco.optionid
    GROUP BY xp.product, xco.optionid
    ORDER by xp.product DESC

In case it’s needed here is the DB layout –

+ xcart_products
    - productid*
    - product
+ xcart_variants
    - variantid*
    - productid (xcart_products.productid)
+ xcart_variant_items [bridge table]
    - optionid*
    - variantid (xcart_variants.variantid)
+ xcart_classes
    - classid*
    - productid (xcart_products.productid)
    - class
+ xcart_class_options
    - optionid*
    - option_name
    - classid (xcart_classes.classid)
+ xcart_images_W
    - imageid*
    - id (xcart_variants.variantid)
    - image_path
+ xcart_images_D
    - imageid* [not relational with xcart_images_W.imageid]
    - id (xcart_products.productid)
    - optionid (xcart_class_options.optionid)

* Primary Key
() relational data
[] notes
  • 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-06-05T16:36:28+00:00Added an answer on June 5, 2026 at 4:36 pm

    Here’s your original query with some notes describing what I think is the problem:

     SELECT xp.productid, xp.product, xc.classid, xco.optionid, xco.option_name, xiW.id, xiW.image_path, t1.dCount
     FROM xcart_products xp
     INNER JOIN xcart_variants xv ON xp.productid = xv.productid
     INNER JOIN xcart_variant_items xvi ON xv.variantid = xvi.variantid
     INNER JOIN xcart_class_options xco ON xvi.optionid = xco.optionid
     INNER JOIN xcart_classes xc ON xco.classid = xc.classid AND xc.class = 'COLOR'
     LEFT JOIN xcart_images_W xiW ON xiW.id = xvi.variantid
     -- all of the above is pretty standard.
    
     -- but this part is a little wonky!
     LEFT JOIN (
     SELECT COUNT(xiD.optionid) as dCount 
        FROM xcart_products xp2
        INNER JOIN xcart_classes xc2 ON xp2.productid = xc2.productid AND xc2.class = 'Color'
        INNER JOIN xcart_class_options xco2 ON xc2.classid = xco2.classid
        LEFT JOIN xcart_images_D xiD ON xiD.optionid = xco2.optionid
     ) as t1 ON xiW.id = xvi.variantid
     -- The inner query here only returns one column, which means it's
     -- *not related* to anything.  That's why, outside, you're using an
     -- ON query that has nothing to do with the data inside the table 
     -- you're joining!  You need to group by in the inner select.
    
     GROUP BY xco.optionid
     ORDER by xp.product DESC
    

    Try something like:

    SELECT xp.productid, xp.product, xc.classid, xco.optionid, xco.option_name, xiW.id, xiW.image_path, t1.dCount
     FROM xcart_products xp
     INNER JOIN xcart_variants xv ON xp.productid = xv.productid
     INNER JOIN xcart_variant_items xvi ON xv.variantid = xvi.variantid
     INNER JOIN xcart_class_options xco ON xvi.optionid = xco.optionid
     INNER JOIN xcart_classes xc ON xco.classid = xc.classid AND xc.class = 'COLOR'
     LEFT JOIN xcart_images_W xiW ON xiW.id = xvi.variantid
     -- Keep the above
    
     -- but change this one to add a group by.
     LEFT JOIN (
     SELECT xco2.optionId as optionid, count(*) as dCount 
        -- this could be cleaned up if the products and class aren't
        -- really relevant- as it is, it serves only to narrow the set
        -- of xcart_class_options you'll be looking at/counting for.
        FROM xcart_products xp2
        INNER JOIN xcart_classes xc2 ON xp2.productid = xc2.productid AND xc2.class = 'Color'
        INNER JOIN xcart_class_options xco2 ON xc2.classid = xco2.classid
    
        -- bring in the image_ds that match the class_option we are looking
        -- at
        LEFT JOIN xcart_images_D xiD ON xiD.optionid = xco2.optionid
    
        -- And group by those option_ids.
        group by xid.optionid
     ) as t1 ON xco.optionid = t1.optionid
     -- we join the inner select based on the optionid we were looking at.
    
     GROUP BY xco.optionid
     ORDER by xp.product DESC
    

    To make it faster, this will probably work, depending on the results you’re looking for:

    SELECT xp.productid, xp.product, xc.classid, xco.optionid, xco.option_name, xiW.id, xiW.image_path, t1.dCount
     FROM xcart_products xp
     INNER JOIN xcart_variants xv ON xp.productid = xv.productid
     INNER JOIN xcart_variant_items xvi ON xv.variantid = xvi.variantid
     INNER JOIN xcart_class_options xco ON xvi.optionid = xco.optionid
     INNER JOIN xcart_classes xc ON xco.classid = xc.classid AND xc.class = 'COLOR'
     LEFT JOIN xcart_images_W xiW ON xiW.id = xvi.variantid
    
     -- since the optionId is on the thing you're counting,
     -- don't bother with any joins in the nested query and just 
     -- count and group.
     LEFT JOIN (
     SELECT xiD.optionId as optionid, count(*) as dCount 
        FROM xcart_images_D xiD
        GROUP BY xiD.optionId
     ) as t1 ON xco.optionid = t1.optionid
     -- still join the inner select based on the optionid
    
     GROUP BY xco.optionid
     ORDER by xp.product DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

New to PHP and MySQL, have heard amazing things about this website from Leo
I want to create a new custom control derived from ContentControl (it will be
I have defined a class A and derived a new class B from A
I understand how the new keyword can hide methods in a derived class. However,
New to Node.js and Express, I am trying to understand the two seems overlapping
I had this idea and my first reaction after having it was That's a
I have a number of classes that derive from a pure virtal base: class
We know the need for a virtual destructor. Base *bptr = new Derived(); delete
Lets say that we have the following code Base() { ... } Derived :
How to use reflection call a base method that is overridden by derived class?

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.