SELECT p.*, a.attribute_id, IFNULL(a.attribute_name,' ') as resolution, CASE WHEN p.product_comingsoon = 1 THEN 1 WHEN p.product_new = 1 THEN 2 WHEN p.product_clearance = 1 THEN 4 WHEN p.product_discontinued = 1 THEN 5 WHEN p.product_outofstock = 1 THEN 6 ELSE 3 END AS sort FROM products as p left join m_product_attribute m ON m.product_id = p.product_id and m.attribute_id in (select attribute_id from attribute where attribute_type_id = 489) left join attribute a on m.attribute_id = a.attribute_id inner join ( m_product_attribute as map0, attribute as arr0) on ( map0.product_id = p.product_id and arr0.attribute_id = map0.attribute_id AND ( arr0.attribute_name="Cameras" ) ) inner join ( m_product_attribute as map1, attribute as arr1) on ( map1.product_id = p.product_id and arr1.attribute_id = map1.attribute_id AND ( arr1.attribute_name="IP" ) ) WHERE p.product_status = 1 AND show_product_list = 1 AND show_on_outlet_store = 0 and product_discontinued <> 1 group by p.product_id ORDER BY sort, p.product_order, resolution DESC, p.product_name ASC