I'm trying to select the latest updated
(field) pages
(table), excluding pages that are hidden (visible
= "n"). However, I want to group pages by slug
so I only get one result per slug.
The following query kinda works, but it only selects the latest updated page for every slug — then, if that page happens to be hidden, it removes it from the results :( In this case I just want to get the latest updated page with that slug that is visible.
SELECT `p1`.`id` AS `pID`, `p1`.`slug` AS `url`, `p1`.`revision`, `p1`.`title`, `p1`.`published`, `p1`.`updated`, (SELECT COUNT(*) FROM `pages` WHERE `slug` = `url` AND `visible` = "y") AS `revisionCount`, (SELECT COUNT(*) FROM `tests` WHERE `pageID` = `pID`) AS `testCount`
FROM `pages` `p1`
LEFT JOIN `pages` `p2` ON `p1`.`slug` = `p2`.`slug` AND `p1`.`updated` < `p2`.`updated`
WHERE `p2`.`updated` IS NULL AND `p1`.`visible` = "y"
ORDER BY `p1`.`updated` DESC