0

我在一个项目中使用 ruby​​ sequel gem 作为我的 ORM,我想使用以下查询来加载一个 sequel 模型:

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 子查询表达式的续集?

4

1 回答 1

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)
  }
于 2013-11-13T16:15:27.737 回答