我有一個用於保存教育體驗的數據庫表。它有4個屬性。如何編寫HQL查詢
- 用戶ID(外鍵涉及用戶表)
- beginDay(入學時)
- endday指定(畢業時間)
- 學校(學校的名稱)
現在我有一個userId,我想根據userId選擇上次的教育體驗。
如何編寫hql查詢?
我已經寫
select * from education where userId = 100 and beginDay = max(beginDay)
但查詢是錯誤的。控制檯輸出無效使用組功能。
我有一個用於保存教育體驗的數據庫表。它有4個屬性。如何編寫HQL查詢
現在我有一個userId,我想根據userId選擇上次的教育體驗。
如何編寫hql查詢?
我已經寫
select * from education where userId = 100 and beginDay = max(beginDay)
但查詢是錯誤的。控制檯輸出無效使用組功能。
只需使用此查詢爲:
SELECT * FROM education WHERE userId = 100 ORDER BY beginDay DESC LIMIT 1
String hql = "select * from education where userId = 100 and beginDay = max(beginDay)";
Query query = session.createQuery(hql);
List results = query.list();
這是完整的HQL查詢語法。 –
請解釋你的答案。 –
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)
我認爲外國並不重要。 –
SELECT * FROM education WHERE userId = 100 ORDER BY beginDay DESC LIMIT 1.此查詢可以選擇userId = 100的最後一次教育體驗嗎?我現在沒有計算機來測試 –
我不是來爲你做你的工作的。 –
它可以選擇userId = 100的最後一次教育體驗嗎? –
它應該按照您的預期工作。 – Legionar