我在项目中使用ruby续集gem作为我的ORM,我想使用以下查询来加载续集模型:
SELECT "subject", "addresses", "personal", "read_by", "folder", "created_at", "updated_at", "sent_at", "draft",
--HOW DO I DO THIS IN Sequel?
EXISTS(SELECT COUNT(id) FROM "todos" WHERE reference_email_id = "emails"."id") as "todos"
FROM "emails"
INNER JOIN "email_participants" ON ("email_participants"."email_id" = "emails"."id") WHERE (("user_id" = 124))
在上面的查询中,这个子查询返回一个布尔结果:
EXISTS(SELECT COUNT(id) FROM "todos" WHERE reference_email_id = "emails"."id") as "todos"
FROM "emails"
目前我的ruby代码看起来像是为了选择除了上面列出的布尔表达式之外的所有内容:
Email.inner_join(:email_participants, {email_id: :id})
.where(user_id: query.user_id)
.select_all(:emails)
如何获得包含此EXISTS子查询表达式的续集?
答案 0 :(得分:1)
我是从记忆中写下来的,所以请原谅我,如果它不能直接起作用的话。但是,我们的想法是使用select_append
添加到所选列。
Email.inner_join(:email_participants, {email_id: :id})
.where(user_id: query.user_id)
.select_all(:emails)
.select_append {
Todo.where(reference_email_id: {emails: :id})
.count(:id)
.exists
.as(:todos)
}