Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.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
Mysql 按最大值排序,按分组_Mysql_Sql_Jpa_Criteria_Jpql - Fatal编程技术网

Mysql 按最大值排序,按分组

Mysql 按最大值排序,按分组,mysql,sql,jpa,criteria,jpql,Mysql,Sql,Jpa,Criteria,Jpql,下表: RECORD --------------------- NAME VALUE --------------------- Bill Clinton 100 Bill Clinton 95 Bill Clinton 90 Hillary Clinton 90 Hillary Clinton 95 Hillary Clinton 85 Monica Lewinsky 70 Monica Lewinsky

下表:

RECORD
---------------------
NAME            VALUE
---------------------
   Bill Clinton   100
   Bill Clinton    95
   Bill Clinton    90
Hillary Clinton    90
Hillary Clinton    95
Hillary Clinton    85
Monica Lewinsky    70
Monica Lewinsky    80
Monica Lewinsky    90
我可以使用JPA(JPQL或标准)选择以下输出吗

   Bill Clinton   100
Hillary Clinton    95
Monica Lewinsky    90
我的意思是,
orderby
maximum
VALUE
groupby
NAME

查询本身

SELECT  Name,
        MAX(value) value
FROM    record
GROUP BY Name
ORDER BY Value DESC
输出:

|            NAME | VALUE |
---------------------------
|    Bill Clinton |   100 |
| Hillary Clinton |    95 |
| Monica Lewinsky |    90 |

我不是jpa方面的专家,但这两条线之间的一些东西可能会起作用

List<Object[]> results = entityManager
        .createQuery("SELECT  Name, MAX(value) maxvalue FROM record GROUP BY Name ORDER BY Value DESC");
        .getResultList();
for (Object[] result : results) {
    String name = (String) result[0];
    int maxValue = ((Number) result[1]).intValue();
}
List results=entityManager
.createQuery(“按名称顺序按值描述从记录组中选择名称、最大(值)最大值”);
.getResultList();
对于(对象[]结果:结果){
字符串名称=(字符串)结果[0];
int maxValue=((数字)结果[1]).intValue();
}
查询本身

SELECT  Name,
        MAX(value) value
FROM    record
GROUP BY Name
ORDER BY Value DESC
输出:

|            NAME | VALUE |
---------------------------
|    Bill Clinton |   100 |
| Hillary Clinton |    95 |
| Monica Lewinsky |    90 |

我不是jpa方面的专家,但这两条线之间的一些东西可能会起作用

List<Object[]> results = entityManager
        .createQuery("SELECT  Name, MAX(value) maxvalue FROM record GROUP BY Name ORDER BY Value DESC");
        .getResultList();
for (Object[] result : results) {
    String name = (String) result[0];
    int maxValue = ((Number) result[1]).intValue();
}
List results=entityManager
.createQuery(“按名称顺序按值描述从记录组中选择名称、最大(值)最大值”);
.getResultList();
对于(对象[]结果:结果){
字符串名称=(字符串)结果[0];
int maxValue=((数字)结果[1]).intValue();
}

由于JPQL查询对实体进行操作,因此假设以下映射:

@Entity
@Table(name="your_table")
public class YourEntity {
    @Id private int id;
    private String name;
    private int value;
    ...
}
对于此类映射,查询如下所示:

SELECT e.name, MAX(e.value)
FROM YourEntity e
GROUP BY e.name
ORDER BY MAX(e.value) DESC

这种查询的结果是对象数组的列表。数组中的第一个元素是name,第二个元素是value(如select中所示)。

由于JPQL查询对实体进行操作,因此假定以下映射:

@Entity
@Table(name="your_table")
public class YourEntity {
    @Id private int id;
    private String name;
    private int value;
    ...
}
对于此类映射,查询如下所示:

SELECT e.name, MAX(e.value)
FROM YourEntity e
GROUP BY e.name
ORDER BY MAX(e.value) DESC
这种查询的结果是对象数组的列表。数组中的第一个元素是name,第二个元素是value(如在select中所示)。

使用条件:

CriteriaQuery q=cb.createQuery(Person.class);
根p=q.from(Person.class);
q、 选择(p.get(“name”)、cb.max(p.get(“value”));
q、 groupBy(p.get(“name”));
q、 orderBy(cb.desc(cb.max(p.get(“value”)));
使用标准:

CriteriaQuery q=cb.createQuery(Person.class);
根p=q.from(Person.class);
q、 选择(p.get(“name”)、cb.max(p.get(“value”));
q、 groupBy(p.get(“name”));
q、 orderBy(cb.desc(cb.max(p.get(“value”)));

“使用JPA(JPQL或标准)”?@eggyal更新了答案“使用JPA(JPQL或标准)”?@eggyal更新了答案