Add condition to sub select in query

巧了我就是萌 提交于 2020-02-06 07:51:15

问题


I have this query

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

How can I get rows only when count is, for example, < 20?


回答1:


you can use group by and having clause.

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


来源:https://stackoverflow.com/questions/59709945/add-condition-to-sub-select-in-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!