简体   繁体   中英

Adding 5th table to join

Maybe it's just too early but I can' figure out why I can't get the query of my fifth table to join with my current query statement.

Here is my current query

SELECT xp.productid, xp.product, xc.classid, xco.optionid, xco.option_name, count(xi.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 xi ON xi.optionid = xco.optionid
GROUP BY xp.product, xco.optionid
ORDER by xp.product DESC

I am trying to add the following query which has a table set up very similar to xcart_images_D

JOIN xcart_images_T xiT ON xi.id = xiT.id

As of now, my results go from about 2,000 to 20 after I add the join.

By doing this:

JOIN xcart_images_T xiT ON xi.id = xiT.id

You are making the JOIN of xcart_images_t dependent on your (xi) xcart_images_D table. So if your xcart_images_D table doesn't match, then your xcart_images_T isn't going to return a match either.

Also by just specifying JOIN, you are implicitly indicating an INNER JOIN, which could also be part of your problem. If having a match in xcart_images_T isn't a requirement, then it should be a LEFT JOIN.

The xcart_images_T seems like it's similar to xcart_images_D, so try a LEFT JOIN and use the xco.optionid as a JOIN key:

LEFT JOIN xcart_images_T xiT ON xiT.id = xco.optionid

Also, if you're expecting to see any fields from xcart_images_T or xcart_images_D, you'll need to add those to your SELECT. See if that helps.

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