简体   繁体   English

在JPQL中使用select unique并检索所有列

[英]Use select distinct in JPQL and retrieve all columns

I'm working with JPQL, I want to remove all the duplicated rows using DISTINCT , and at the same time retrieve all the columns in a table, I wrote something like this: 我正在使用JPQL,我想使用DISTINCT删除所有重复的行,并同时检索表中的所有列,我这样写:

SELECT DISTINCT cl.name, cl.age 
FROM Client AS cl
WHERE cl.country='****'

This query returns just the two columns name and age . 该查询仅返回两列nameage

Assuming you have a unique id you could write your query to use GROUP BY as follows: 假设您具有唯一的id ,则可以编写查询以使用GROUP BY ,如下所示:

SELECT client FROM Client client
WHERE client.id IN (
    SELECT MIN(c.id)
    FROM Client c
    WHERE c.country='****'
    GROUP BY c.name, c.age
)

You should not retrieve all the fields of Client because you should not select non-aggregated fields. 您不应检索Client所有字段,因为您不应选择非汇总的字段。

Try this : 尝试这个 :

DELETE from Client c
Where c.name IN (SELECT DISTINCT cl.name
                 FROM Client AS cl
                 WHERE cl.country='****')

But pay attention to your persistence context to avoid corrupt data. 但是请注意您的持久性上下文,以免损坏数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM