wow really odd i cannot figure it out.
Works fine with the limit just order by causes the issue.
$sql = '
SELECT i.*, r.rating, r.item_id, ROUND(AVG(r.rating), 0) as averageRating, child_cat.name AS catname, child_cat.icon AS icon
FROM chewi_m_items_categories parent_cat
INNER
JOIN chewi_m_items_categories child_cat
ON child_cat.parent_id = parent_cat.id
INNER
JOIN chewi_m_items i
ON i.cat_id = child_cat.id
AND i.status = 1
LEFT OUTER
JOIN chewi_m_items_reviews r
ON r.item_id = i.id
WHERE parent_cat.id = :cat_id GROUP BY i.id
UNION ALL
SELECT i.*, r.rating, r.item_id, ROUND(AVG(r.rating), 0) as averageRating, child_cat.name AS catname, child_cat.icon AS icon
FROM chewi_m_items_categories child_cat
INNER
JOIN chewi_m_items i
ON i.cat_id = child_cat.id
AND i.status = 1
LEFT OUTER
JOIN chewi_m_items_reviews r
ON r.item_id = i.id
WHERE child_cat.id = :cat_id GROUP BY i.id
' . $limit;
I also tested one statement on its own and removed from the union and it works fine but as soon as i readd it and do an order by it breaks! Strange.
By adding the order by, it causes an bool(false)
update: when i add order by averageRating it works but as soon as i do i.something it breaks so i guess i should do an i.id as itemid or something… nope that breaks it, how odd
UPDATE: FIXED IT, It did not want i.id just simply id:
SELECT i.*, r.rating, r.item_id, ROUND(AVG(r.rating), 0) as averageRating, child_cat.name AS catname, child_cat.icon AS icon
FROM chewi_m_items_categories parent_cat
INNER
JOIN chewi_m_items_categories child_cat
ON child_cat.parent_id = parent_cat.id
INNER
JOIN chewi_m_items i
ON i.cat_id = child_cat.id
AND i.status = 1
LEFT OUTER
JOIN chewi_m_items_reviews r
ON r.item_id = i.id
WHERE parent_cat.id = :cat_id GROUP BY i.id
UNION ALL
SELECT i.*, r.rating, r.item_id, ROUND(AVG(r.rating), 0) as averageRating, child_cat.name AS catname, child_cat.icon AS icon
FROM chewi_m_items_categories child_cat
INNER
JOIN chewi_m_items i
ON i.cat_id = child_cat.id
AND i.status = 1
LEFT OUTER
JOIN chewi_m_items_reviews r
ON r.item_id = i.id
WHERE child_cat.id = :cat_id GROUP BY i.id ORDER BY id DESC
Thanks again for all your help