【问题标题】:Esqueleto count inside select选择内的 Esqueleto 计数
【发布时间】:2016-11-19 00:19:46
【问题描述】:

我有以下Entities

Group
  name Text

GroupUser
  user UserId
  group GroupId

我想做这样的查询:

select g.* /* Everything from g */
     , count(gu.id) groupUsersCount 
  from Group g
  left outer join GroupUser gu on gu.groupId = g.id
group by g.id

可以用Esqueleto完成吗?

【问题讨论】:

    标签: sql haskell yesod esqueleto


    【解决方案1】:

    esqueleto docs for groupBy 包含如何使用它的好例子。

    此外,通过阅读Getting Started 部分,您将看到几个查询示例,包括与table.* 等效的查询:

    do people <- select $
                 from $ \person -> do
                 return person
    

    将两者放在一起意味着这样的事情应该可以工作:

    select $ from \(g `LeftOuterJoin` gh) -> do
      on (gu ^. GroupId ==. g ^. Id)
      groupBy (g ^. Id)
      return (g, countRows)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-08
      • 1970-01-01
      • 2014-02-26
      • 1970-01-01
      • 2015-08-27
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      相关资源
      最近更新 更多