作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 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/
我正在处理一个页面,其中列表中的所选元素的特点是没有给定的类。类似于以下内容: not selected selected not selected 我想定义一个 CSS 选择器来获取
我正在尝试为我的 iOS 应用创建/导出一个独有的 UTI 类型(非常类似于 Instagram 独占处理 UTI com.instagram.exclusivegram 的方式)。 基本上,我希望
我是一名优秀的程序员,十分优秀!