I’ve got two tables (MySQL), one with a list of categories and another with a list of products which can be assigned to a category. I need to pull out the date of the most recent change across both tables so I can reset the cache for this page if needed.
I seem to be struggling with using MAX() in a CASE, currently I have this which works but the product date is just the latest one entered into the database and not necessarily the most recent.
SELECT c.pageid as caturl, p.updated as pup, c.updated as dup,
CASE WHEN p.updated > c.updated THEN p.updated ELSE c.updated END AS latest
FROM products p, categories c
WHERE p.catid = c.id AND c.hide=0
GROUP BY c.title
When I try to use MAX() within the CASE it throws an error, as it does when using an IF.
I’d appreciate any help, thanks.
Ok, I’m taking a stab at this, two derived tables.
One, that calculate the MostRecent updated date for each category in the categories table: “catdates” (and also returns the pageid)
The Other, calculates the MostRecent updated date across all products in a given category for each category in the products table: “proddates”
Then we just join on categoryid and take the higher value.