在某些條件下COUNT且指定欄位value重複只算一次: DB::raw('COUNT(DISTINCT IF(contact_result.invalid_flag = "Y", customer.customer_id, NULL)) AS contact_invalid_count')