我有一个数据库表,用于保存教育经验。它有4个属性。
现在我有了userId,我想根据userId选择最后的教育体验。
如何编写hql查询?
我写过
select * from education where userId = 100 and beginDay = max(beginDay)
但查询错误。控制台输出无效使用组功能。
答案 0 :(得分:0)
只需使用此查询:
SELECT * FROM education WHERE userId = 100 ORDER BY beginDay DESC LIMIT 1
答案 1 :(得分:0)
FROM education e where e.UserId = 100 and e.beginDay = max(e.beginDay)
应该有效。我不确切知道你的数据库看起来如何
如果我没记错的话,如果是外键值,则需要获得完整的类结构。已经有一段时间......
FROM education e where e.User.UserId = 100 and e.School.beginDay = max(e.beginDay)
答案 2 :(得分:-2)
String hql = "select * from education where userId = 100 and beginDay = max(beginDay)";
Query query = session.createQuery(hql);
List results = query.list();