I have some sql that gathers product information. It also retrieves images from joined tables. Sometimes a product will have more than one image (sometimes none) and this causes duplicates in the results. How can I change this query to only return a single image (or no images) from the joined image tables – image_d,image_p,image_t ?
SELECT
products_categories.categoryid,
products.productid,
products.product,
products.descr,
products.rating,
products.title_tag,
images_d.image_path AS imaged,
images_p.image_path AS imagep,
images_t.image_path AS imaget,
clean_urls.clean_url
FROM products_categories
INNER JOIN products ON products_categories.productid = products.productid
LEFT JOIN images_d ON products.productid = images_d.id
LEFT JOIN images_p ON products.productid = images_p.id
LEFT JOIN images_t ON products.productid = images_t.id
LEFT JOIN clean_urls ON products.productid = clean_urls.resource_id
WHERE products_categories.categoryid = 265
AND products_categories.main = 'Y'
AND products.forsale = 'Y'
ORDER BY productid
Thanks.
I assume that there’s more than one image in a given image_x table. You can use
group_concat()to shoehorn multiple values into one CSV column: