2

假设我有三个表用户、产品和用户产品。一旦用户购买了产品,就会在 UserProduct 表中创建一条记录。我想计算用户购买的平均产品数量。我可以在 mySql RDBMS 上使用纯 SQL 来执行此操作,如下所示:

select avg(c) from (select count(*) as c from user_product up join user u on (up.user_id = u.id) group by u.id) as res

我有机会用 JPA 做同样的事情吗?

更新:我知道子查询只能在 WHERE 或 HAVING 子句中使用,但仍然......

4

0 回答 0