如何在hql中编写group?

时间:2016-01-29 09:31:14

标签: hibernate group-by hql

我写了一个像这样的hql:

select DATE_FORMAT(createAt, '%Y-%m-%d') as day, sum(amount) as orderAmount 
    from FoodOrder where deleted = 'false' and orderState = ?1
    and createAt between ?2 and ?3 group by day order by day

我收到了错误:“群组声明”中的“未知列”日期。

这样的hibernate日志:

select
    date_format(foodorder0_.create_at,
    '%Y-%m-%d') as col_0_0_,
    sum(foodorder0_.amount) as col_1_0_
from
    food_order foodorder0_
where
    foodorder0_.deleted='false'
    and foodorder0_.order_state=?
    and (
        foodorder0_.create_at between ? and ?
    )
group by
    day
order by
    col_0_0_

为什么订单使用col_0_0_但使用“day”分组并得到错误?

任何帮助谢谢!

1 个答案:

答案 0 :(得分:0)

是的,正如Bonifacio的回答,它应该是:

group by DATE_FORMAT(createAt, '%Y-%m-%d')

很抱歉迟到的评论,这次我不能将Bonifacio设为最好的答案。