$sql = "SELECT p.product_id,
(SELECT sort_order FROM " . DB_PREFIX . "category WHERE category_id =
(SELECT c.parent_id FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "product_to_category p2cc ON (c.category_id = p2cc.category_id) WHERE p2cc.product_id = p.product_id AND c.parent_id != '0' ORDER BY c.sort_order ASC)
) AS category,