Java hql case-when usage
if (null = = Sorter) { hql.append ("ORDER by m.mdate desc,case if m.mealtime = ' Morning ' then ' 1 ' when m.mealtime = ' Noon ' Then ' 2 ' when m.mealtime = ' Night ' and ' 3 ' when m.mealtime = ' midnight ' then ' 4 ' End '); }
Note:
The meaning of the above HQL statement can be understood as:
By mdate descending order, but also to meet when mealtime = ' morning ', on the basis of the date first, when mealtime = ' noon ' the same, and so on.
A case can be written after a select, or it can be written after the order by
Here's how SQL's case is written
Select T.id,t.name,t.code,t.select_time,t.now_level, CaseT.now_level when ' County level leadership position ' then 38 when' County level non-leadership position ' then 36 when' deputy leadership positions at county level ' then 34 when' Deputy non-leadership position at county level ' then 31 when' Township Ke ji ' leading position ' then 28 when' Township ke ji non-leadership position ' then 26 when' Xiang Ke ji deputy non-leading position ' then 21 when' Clerk ' then 20 when' New Hire undecided ' then 19 when' Level five staff ' then 18 when' Level six staff ' then 15 when' Level seven staff ' then 13 when' Level eight staff ' then 11 when' Level nine staff ' then 9 when' City Tube business ' then 6 when' City Tube Business deputy ' then 3 when' Enterprise Other ' then 1END levefrom torg_cadre t WHERE t.status=2ORDER by code DESC, (Date_format (select_time, '%Y ')) desc, leve desc;
Multi-field ordering, followed by DESC or ASC for each field
About the HQL statement in Hibernate