SELECT a.id, a.is_group_buy, IF (a.is_group_buy = 'Yes', NULL, c.name) as cn FROM order_info a JOIN ( SELECT user_id FROM order_info WHERE status = 'completed' GROUP BY user_id HAVING ...