SELECT *,
sum(
case when ( ( products_vendors.quantity > 0 )
and ( ( product_vendors.vendor_id <> 'X' )
or ( ( products_vendors.vendor_id = 'X' )
and ( products.type = 'X' )
)
)
) then 1 else 0 end
) as sum1,
FROM products
LEFT JOIN products_vendors
ON products.id=products_vendors.product_id
WHERE product_vendors.vendor_id IN (‘a’, ‘b’, ‘x’)
GROUP BY products.id
HAVING ( sum1 > 0 );