Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.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 我可以按JPA中的顺序求和值吗?_Mysql_Spring_Hibernate_Jpa - Fatal编程技术网

Mysql 我可以按JPA中的顺序求和值吗?

Mysql 我可以按JPA中的顺序求和值吗?,mysql,spring,hibernate,jpa,Mysql,Spring,Hibernate,Jpa,我正在开发一个Spring Boot应用程序,我使用JPA。我的问题是: 我有3个字段(点1、点2和点3),我正在进行排名。所以,我想通过这三个字段的总和来获得排名的第一名 要理解的示例: SELECT u FROM User u ORDER BY u.totalPoints DESC; Where u.totalPoints is the sum of points1, points2 and points3. 我怎么做?谢谢您需要在此处进行相关查询: SELECT u FROM User

我正在开发一个Spring Boot应用程序,我使用JPA。我的问题是:

我有3个字段(点1、点2和点3),我正在进行排名。所以,我想通过这三个字段的总和来获得排名的第一名

要理解的示例:

SELECT u FROM User u ORDER BY u.totalPoints DESC;
Where u.totalPoints is the sum of points1, points2 and points3.

我怎么做?谢谢

您需要在此处进行相关查询:

SELECT u FROM User u 
Where u.totalPoints = (select uu.points1 + uu.points2 + uu.points3 
                       from User uu 
                       where uu.id = u.id)
ORDER BY u.totalPoints DESC;

您可以在
SELECT
子句中为表达式定义别名,并在
ORDER BY
子句中使用这些别名:

SELECT *, (points1 + points2 + points3) AS total_points 
FROM user 
ORDER BY total_points DESC 

这三个字段是哪个实体的一部分?同一个实体,用户我需要将查询(MySQL)转换为JPA:SELECT*,(
LEAGUE\u POINTS
+
FORTNITE\u POINTS
+
TFT\u POINTS
)作为来自用户的总积分,按总积分排序,我在MySQL中尝试了它,效果良好,但是你知道我如何在JPA中做到这一点吗?@CarlosDevesa对不起,不知道;但这应该不会太难,他们又来了,我明白了。我将EntityManager调用createQuery更改为createNativeQuery,并放入默认的MySQL代码。谢谢我的字段totalPoints未在数据库中定义,因此无法使用。我可以为总和创建一个时间变量吗?