【问题标题】:Jpql: Having without Group ByJpql:没有分组依据
【发布时间】:2021-01-27 23:08:27
【问题描述】:

我有(工作的)SQL 查询

   select team
   from team
   join account a on team.account_id = a.id
   left join exchange_order eo on a.id = eo.account_id
   left join exchange_order_transaction eot on eo.id = eot.order_id
   having count(eot.id) = 0;

想把它翻译成jpql

    @Query("SELECT COUNT(t) " +
            "FROM Team t " +
            "         left join Account a on t.account = a.id " +
            "         left join ExchangeOrder eo on a.id = eo.account " +
            "         left join ExchangeOrderTransaction eot on eo.id = eot.order " +
            "HAVING count(eot) = 0")
    long countWithoutTransactions();

问题在于,JPQL 似乎不允许在没有“分组依据”的情况下使用 HAVING。但在那种情况下 我不想分组任何东西。只想计算所有行。

有什么方法可以解释“分组依据”或我应该如何构造该查询?

【问题讨论】:

  • 请解释having count(eot.id) = 0背后的逻辑……这应该是什么断言?
  • 团队通过 exchange_order 和 exchange_order_transaction 的账户连接。我想让所有团队在 exchange_order_transaction 表中没有任何行。

标签: postgresql jpql


【解决方案1】:

我认为您只想对exchange_order_transaction 表进行左反连接。使用这个查询:

select team.*
from team
join account a on team.account_id = a.id
left join exchange_order eo on a.id = eo.account_id
left join exchange_order_transaction eot on eo.id = eot.order_id
where eot.id is null;

【讨论】:

  • 不,不完全是。我想要 COUNT 个没有 exchange_order_transaction 的团队。我的 SQL 查询运行良好,只是不知道如何将其转换为 JPQL。就像我在 SQL 中所做的那样,如何使用 HAVING 和不使用 GROUP BY 来制作 JPQL。
  • 在没有GROUP BY 的情况下使用COUNT 完全没有意义。此时,您应该将示例输入和输出数据添加到您的问题中,以更好地解释您在此处尝试执行的操作。
猜你喜欢
  • 2015-01-02
  • 2017-10-18
  • 2011-05-31
  • 2016-05-05
  • 2015-02-04
  • 2012-06-19
  • 2013-01-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多