Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JPQL选择查询,无法按分组_Java_Mysql_Jpa_Jpql - Fatal编程技术网

Java JPQL选择查询,无法按分组

Java JPQL选择查询,无法按分组,java,mysql,jpa,jpql,Java,Mysql,Jpa,Jpql,几个小时后,我似乎找不到实现这一点的方法,我有两个实体: public class Price{ @Id int id; @Temporal(TemporalType.DATE) private Date dtComu; private String descCarb; private Double price; //bi-directional many-to-one association to Distributor @ManyToOne @JoinColumn(name="idI

几个小时后,我似乎找不到实现这一点的方法,我有两个实体:

public class Price{
@Id
int id;

@Temporal(TemporalType.DATE)
private Date dtComu;

private String descCarb;

private Double price;

//bi-directional many-to-one association to Distributor
@ManyToOne
@JoinColumn(name="idImpiant")
private Distributor distributor;

我得到的错误是:

SELECT列表不在GROUP BY子句中,并且包含未聚合的 列“price0.id”,它在功能上不依赖于中的列 分组依据子句


有没有其他我没有想到的方法来实现这一点?

您看到的错误源于不清楚每个分销商组需要哪个实体。以下是使用HQL执行此操作的一种方法:

select e FROM Price e JOIN e.distributor d
where e.descCarb like '%Diesel%' and
      e.dtComu = (select max(ee.dtComu) from Price ee where ee.distributor = e.distributor)

这将使用一个相关子查询来检查匹配的
Price
实体是否是每个分销商的最新实体。

这就像魔术一样有效!我还有一个问题,有时候我会得到两个相同的分销商的价格,因为一个是柴油,另一个是蓝色柴油。在本例中,我只是按价格进行选择,是用java修改返回列表,还是有办法编辑查询?使用HQL很难实现这一点(尽管在一般SQL中很容易)。这听起来更像是一个后续问题。
SELECT e FROM Price e JOIN e.distributor d WHERE e.descCarb like '%Diesel%' group by e.distributor order by e.dtComu desc
select e FROM Price e JOIN e.distributor d
where e.descCarb like '%Diesel%' and
      e.dtComu = (select max(ee.dtComu) from Price ee where ee.distributor = e.distributor)