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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:45:34+00:00 2026-06-03T15:45:34+00:00

This already involves four INNER JOINs and one LEFT JOIN, but I am not

  • 0

This already involves four INNER JOINs and one LEFT JOIN, but I am not sure how I can reference one more table that is similar to the LEFT JOIN table.

Here is my current query which works as it is suppose to

 SELECT xp.productid, xp.product, xc.classid, xco.optionid, xco.option_name, xiW.id, xiW.image_path, count(xvi.variantid) as cnt
    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
    GROUP BY xco.optionid
    ORDER by xp.product DESC

So essentially each product has both a class and a variant, then each class has class options, and each variant has variant items. The variants have their own variant image and each class has it’s own ‘detailed’ image (xcart_images_D). As of now I am referencing the variant images but what I would like to do is instead of counting xvi.variantid I want to count how many detailed images there are for each of optionid.

I have a query which will do this but unfortunately I cannot use this query to pull xcart_images_W. That query is belo

 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

Is it possible to reference these two tables or do their foreign keys make it impossible to do so?

Below I have provided the table structures.

+ 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)
    - image_path

* Primary Key
() relational data
[] notes

Currently working but messy query. How do I clean up?

 SELECT xp.productid, xp.product, xc.classid, xco.optionid, xco.option_name, xiW.id, xiW.image_path
 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
 ) ON xiW.id = xvi.variantid
 GROUP BY xco.optionid
 ORDER by xp.product DESC
  • 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-03T15:45:37+00:00Added an answer on June 3, 2026 at 3:45 pm

    It’s not entirely clear until you provide table definition. Have you tried replacing count(xvi.variantid) with count(xiW.id) in the first query?

    updated:

    SELECT xp.productid, xp.product, xc.classid, xco.optionid, xco.option_name, xiW.id, xiW.image_path, count(xvi.variantid) as cnt
    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 (
       SELECT COUNT(xiD.id) as Dcount 
       FROM xcart_images_D xiD 
       INNER JOIN xcart_images_W xiW ON xiW.column = xiD.column /*please update accordingly */
     ) ON xiW.id = xvi.variantid 
    GROUP BY xco.optionid
    ORDER by xp.product DESC
    

    updated again:

    SELECT xp.productid, xp.product, xc.classid, xco.optionid, xco.option_name, xiW.id, xiW.image_path, count(xvi.variantid) as cnt, xiD.totalD
    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 (
       SELECT count(xiD.id) as totalD, xiD.optionid FROM xcart_images_D xiD WHERE xiD.optionid = xvi.optionid
    ) ON xiD.optionid = xvi.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

There's this already populated database which came from another dev. I'm not sure what
I'm fighting with this already 1 and a half hour, but can't find a
This has already been asked here , but I am just not satisfied with
May be I missed something, but I've already beat my head with this one.
The question here is whether something like this already exists or, if not, whether
There's a question on this already (here) . But I'm hoping that because I'm
I'm already tossing around a solution but as I haven't done something like this
We've recently upgraded one of our projects. This involves new versions of JARs also.
Part of this question is I'm not even sure what exactly I'll need to
Well, I'm sure this question has been asked before but I'm yet to find

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.