
Originally Posted by
downtroden
Adding GROUP BY createdAt just doesn't seem correct.
yeah, it isn't -- you want to GROUP BY the category, to get all the roles for the category
Code:
SELECT u.firstName
, u.lastName
, u.email
, b.intro AS copy
, b.createdAt
, b.title
, b.id AS blogId
, b.categoryId
, 'blog' AS type
, r.view_roles
FROM blogs b
INNER
JOIN users u
ON u.id = b.userid
INNER
JOIN ( SELECT categoryid
, CAST(GROUP_CONCAT(roleid) AS CHAR) AS view_roles
FROM roles
WHERE type = 'view'
GROUP
BY categoryid ) AS r
ON r.categoryid = b.categoryid
UNION ALL
SELECT u.firstName
, u.lastName
, u.email
, c.comment AS copy
, c.createdAt
, b.title
, c.blogid
, b.categoryId
, 'comment' AS type
, r.view_roles
FROM comments c
INNER
JOIN users u
ON u.id = c.userid
INNER
JOIN blogs b
ON b.id = c.blogid
INNER
JOIN ( SELECT categoryid
, CAST(GROUP_CONCAT(roleid) AS CHAR) AS view_roles
FROM roles
WHERE type = 'view'
GROUP
BY categoryid ) AS r
ON r.categoryid = b.categoryid
ORDER
BY createdAt DESC LIMIT 10
note also it's UNION ALL
Bookmarks