Im trying to do a LEFT JOIN/INNER JOIN/JOIN (forgot the differences, my MySQL knowledge got dusty…) on two tables. one is categories and the other is images .
I want to return a list of all categories with a certain parent value, and for each of these categories return the latest image from images for that category.
I ended up with this query:
SELECT * FROM `categories` AS `a`
JOIN (SELECT `im`.`preview` AS `preview`,
`im`.`cid` AS `cid`
FROM `images` AS `im`
ORDER BY `im`.`cid` DESC) AS `b`
ON (`a`.`cid` = `b`.`cid`)
GROUP BY `preview`
This returns a single image from the images table for each row in the category table,
But if I’d try to condition/sort the resulting table, I’ll start getting weird results,
for example changing the end to this or any similar variation:
WHERE `a`.`parent` = 10
GROUP BY `preview`
ORDER BY `a`.`cid
Would appreciate any ideas you would have on this. Thanks in advanced.
You can do it also without join, just using subselect: