2 u010369832 u010369832 于 2016.03.03 18:08 提问

sql转换成hql报错。。。。?

unexpected token: ( near line 1, column 59 [from com.esoft.archer.user.model.UserBill bill inner join (select user.id,max(seqNum) as seq from com.esoft.archer.user.model.UserBill group by user.id)tmp where bill.user.id=tmp.user.id and bill.seqNum=tmp.seq order by bill.user.id]; nested exception is org.hibernate.hql.ast.QuerySyntaxException: unexpected token: ( near line 1, column 59 [from com.esoft.archer.user.model.UserBill bill inner join (select user.id,max(seqNum) as seq from com.esoft.archer.user.model.UserBill group by user.id)tmp where bill.user.id=tmp.user.id and bill.seqNum=tmp.seq order by bill.user.id]

5个回答

princess4426
princess4426   2016.03.03 18:25

贴一下hql看看,报错看不懂

u010369832
u010369832 from UserBill bill inner join (select user.id,max(seqNum) as seq from UserBill group by user.id)tmp where bill.user.id=tmp.user.id and bill.seqNum=tmp.seq order by bill.user.id
接近 2 年之前 回复
haoxin1210
haoxin1210   2016.03.03 22:37

贴源码,别贴报错。,贴报错,谁能看懂

u010369832
u010369832 from UserBill bill inner join (select user.id,max(seqNum) as seq from UserBill group by user.id)tmp where bill.user.id=tmp.user.id and bill.seqNum=tmp.seq order by bill.user.id
接近 2 年之前 回复
a510835147
a510835147   2016.03.04 08:47

你的hql应该是
from UserBill bill inner join (select user.id,max(sqlNum) as seq from UserBill group by user.id)**tmp** where bill.user.id=tmp.user.id and bill.seqNum=tmp.seq order by bill.user.id

tmp前面加个空格看看。。 还有 where改成on看看,上面加粗的地方哦

u010369832
u010369832 不行啊,还是报这个错。[ERROR] [http-bio-8080-exec-9] [02:10:17] org.hibernate.hql.PARSER - line 1:59: unexpected token: ( [ERROR] [http-bio-8080-exec-9] [02:10:17] org.hibernate.hql.PARSER - line 1:75: unexpected token: max [ERROR] [http-bio-8080-exec-9] [02:10:17] org.hibernate.hql.PARSER - line 1:59: unexpected token: ( [ERROR] [http-bio-8080-exec-9] [02:10:17] org.hibernate.hql.PARSER - line 1:75: unexpected token: max
接近 2 年之前 回复
u010369832
u010369832   2016.03.04 10:10

源码:from UserBill bill inner join (select user.id,max(seqNum) as seq from UserBill group by user.id)tmp where bill.user.id=tmp.user.id and bill.seqNum=tmp.seq order by bill.user.id

princess4426
princess4426   2016.03.17 15:39

你给的hql不完整啊。首先 join 之后应该用 on,而不是用 where,这个不会报错。
但是tmp子查询里,怎么会有 user.id 这样的查询?最后在执行 order by 时,被排序的字段不应该是有别名的,因为执行 order by 时,select 已经执行结束了,所以它应该是有个列名。
我觉得你应该想要的样子是这样。

 select bill.id as id, bill.seqNum as seqNum
from (
    select id, seqNum
    from UserBill
) bill join (
    select id, max(seqNum) as seq
    from UserBill
    group by id
) tmp on(bill.id = tmp.id and bill.seqNum = tmp.seq)
order by id

逻辑上也没看明白你想要做什么,但是语法上这样就对了。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!