gpt4 book ai didi

jpa - JPA 中的 ORDER BY "CASE column"

转载 作者:行者123 更新时间:2023-12-04 11:13:46 25 4
gpt4 key购买 nike

我想按 case 语句排序,这可能吗?我该怎么做?

SELECT new com.systemname.to.UserDataHolder(u.userId, 
CASE WHEN (u.place.cityId = :cityId) THEN 1 WHEN (u.place.stateId = :stateId) THEN 2 ELSE 3 END)
FROM User u
ORDER BY u.userId DESC

最佳答案

您使用的是什么 JPA 提供程序?

尝试,

SELECT u.userId, 
(CASE
WHEN (u.place.cityId = :cityId) THEN 1
WHEN (u.place.stateId = :stateId) THEN 2
ELSE 3 END) as myNum
FROM User u
ORDER BY u.userId, myNum DESC

或者,
SELECT new com.systemname.to.UserDataHolder(u.userId, 
CASE
WHEN (u.place.cityId = :cityId) THEN 1
WHEN (u.place.stateId = :stateId) THEN 2
ELSE 3 END)
FROM User u
ORDER BY u.userId, CASE
WHEN (u.place.cityId = :cityId) THEN 1
WHEN (u.place.stateId = :stateId) THEN 2
ELSE 3 END DESC

关于jpa - JPA 中的 ORDER BY "CASE column",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7958455/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com