edit: Now for some reason each productid is only showing up once instead of depending on how many variations it has? Any ideas? (The query I am using is the last query I have listed)
Here is my three table join query
SELECT xp.productid, xp.product, xc.classid, xco.optionid, xco.option_name
FROM xcart_products xp
JOIN xcart_classes xc ON xp.productid = xc.productid and xc.class = 'Color'
JOIN xcart_class_options xco ON xc.classid = xco.classid
ORDER by xp.product DESC
Now everything here works great, I get about 2,000 results, everything is organized and arranged correctly.
Now I would like to add a fourth table to run a count of the number of xi.optionid per xi.id, but for some reason I am only getting about 200 results and can't figure out why. Here is the query I am attempting to run
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
INNER JOIN xcart_images_D xi ON xi.optionid = xco.optionid
GROUP BY xp.product
ORDER by xp.product DESC
Here's the basic schema for each of the tables
xcart_products -
+ productid*
+ product
xcart_classes -
+ classid*
+ productid (xcart_products.productid)
xcart_class_options -
+ optionid*
+ classid (xcart_classes.classid)
+ option_name
xcart_images_D -
+ imageid*
+ optionid (xcart_class_options.optionid)
+ id (xcart_products.productid)
Query I'm currently using -
SELECT xp.productid, xp.product, xc.classid, xco.optionid, xco.option_name, count(xi.optionid) as cnt
FROM xcart_products xp
JOIN xcart_classes xc ON xp.productid = xc.productid AND xc.class = 'Color'
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
ORDER by xp.product DESC
Try using LEFT OUTER JOIN
instead.
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 outer join xcart_images_D xi on xi.optionid = xco.optionid
group by xp.product
order by xp.product desc
group by xp.product, xp.productid
- RedFilter 2012-04-05 17:17
User a Left join on the last table.