向查询中的子选择添加条件

问题描述 投票:0回答:1

我有这个查询

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时如何才能获得行?

database postgresql postgresql-9.4
1个回答
0
投票

您可以使用group byhaving子句。

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
© www.soinside.com 2019 - 2024. All rights reserved.