以下查询在SequelPro中执行时有效:
SELECT * FROM friend_computer LEFT JOIN computer m ON computer_id = m.id LEFT JOIN computer_status ms ON m.id = ms.computer_id JOIN friend_account pa ON friend_account_id = pa.id WHERE friend_account_id = 1 ORDER BY coalesce(ms.last_connect_time < ms.last_disconnect_time, -1) asc;
但是在Hibernate中,我不断收到以下语法错误:
String sql2 = "SELECT pm FROM FriendComputer pm LEFT JOIN FETCH pm.computer m JOIN FETCH pm.friendAccount pa " +
"WHERE pm.friendAccount.id = :friendId AND pm.computer = m ORDER BY coalesce((m.computerStatus.lastDisconnectTime < m.computerStatus.lastConnectTime),-1 ) " + sortOrder;
org.hibernate.query.Query q = session().createQuery(sql2).setParameter("friendId", friendId);
语法错误是由于合并功能引起的。我找不到有关如何在合并中使用表达式的在线资源。
答案 0 :(得分:1)
您想做什么?明确的case
至少可以提供清晰的信息:
order by (case when ms.last_connect_time < ms.last_disconnect_time then 2
when ms.last_connect_time >= ms.last_disconnect_time 1
else 0
end)