gpt4 book ai didi

sql - 独家加盟有很多关系

转载 作者:行者123 更新时间:2023-11-29 12:13:30 25 4
gpt4 key购买 nike

我有一个 Postgres 表,其中用户可以有许多付款订阅,并且付款订阅处于事件状态或非事件状态,这通过付款订阅表上的 activated_at 和 inactivated_at 日期字段显示。我现在想找到所有没有有效付款订阅的用户。

找到所有没有付费订阅的用户很容易

User.joins("LEFT JOIN payment_subscriptions ON (payment_subscriptions.user_id = users.id)").where("payment_subscriptions.id IS NULL")

我还想找到所有只有非事件订阅的用户。如果我使用与上面相同的模式,我会找到所有在某个时候停用订阅的用户,但不能保证他们也没有事件订阅。

User.joins("LEFT JOIN payment_subscriptions ON (payment_subscriptions.user_id = users.id)").where("payment_subscriptions.inactivated_at IS NOT NULL")

如何从我的表中获取没有订阅或没有有效订阅的用户?

最佳答案

这个为您提供没有任何有效订阅的用户。

User.joins("LEFT JOIN payment_subscriptions on (payment_subscriptions.user_id = users.id and payment_subscriptions.inactivated_at is null)").where("payment_subscriptions.id is null")

如果你只想得到那些不活跃的人,也可以使用你以前的加入

我不知道在 rails 中是否可行,但你也可以使用 not exists 语法:

User.where('
not exists (
select *
from payment_subscriptions as ps
where ps.user_id = users.id and ps.inactivated_at is not null
)
')

关于sql - 独家加盟有很多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18403310/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com