У меня есть этот запрос
SELECT DISTINCT u.email, ( SELECT count(DISTINCT o.id) FROM orders o INNER JOIN cart_dates cd ON cd.order_id = o.id WHERE u.id = o.user_id ) as count FROM users u
Как получить строки, только если count, например, <20? </p>
count
Вы можете использовать предложения group by и having.
group by
having
select u.email from users u inner join orders o on o.user_Id = u.id inner join card_dates cd on cd.order_id = o.id group by u.email having count(distinct o.id) < 20