Is there a more efficient way to do this without counting twice?
i.e returning a warning image string if the count is 0 otherwise return the count.
IF(
((SELECT COUNT(pc.category_id) from product_categories pc WHERE pc.product_id = cp.ItemID) > 0),
(SELECT COUNT(pc.category_id) from product_categories pc WHERE pc.product_id = cp.ItemID),
"<img src=\"_imgs/warning.png\" alt=\"warning\"/>"
) AS category_count
Use variable for it. (But I think its result may be cached by query cache.)