我有下列情况。
我想在时间t1和时间t2之间检索出价列表。然后从这个列表中我想检索中标,即最高出价。
我编写了以下JPA查询。
SELECT b FROM Bid b WHERE b.bidAmt = (SELECT MAX(b.bidAmt) FROM b WHERE b.lastUpdtTs BETWEEN ?1 AND ?2)
但我得到以下异常。
Exception Description: Syntax error parsing the query [SELECT b FROM Bid b WHERE b.bidAmt = (SELECT MAX(b.bidAmt) FROM b WHERE b.lastUpdtTs BETWEEN ?1 AND ?2)], line 1, column 64: unexpected token [b].
Internal Exception: NoViableAltException(66!=[1108:1: subselectIdentificationVariableDeclaration[List varDecls] : ( identificationVariableDeclaration[varDecls] | n= associationPathExpression ( AS )? i= IDENT | n= collectionMemberDeclaration );])
有人指出这个错误吗?
答案 0 :(得分:1)
我自己没有尝试过,但从我看到的情况来看,错误可能出现在你的子查询中。您说FROM b
,但它应该是FROM Bid b
。因此,整个查询如下所示:
SELECT b FROM Bid b WHERE b.bidAmt = (SELECT MAX(b.bidAmt) FROM Bid b WHERE b.lastUpdtTs BETWEEN ?1 AND ?2)
答案 1 :(得分:1)
由于您查询引用两个不同的Bid
实例,因此它们应具有不同的别名:
SELECT b FROM Bid b WHERE b.bidAmt =
(SELECT MAX(bb.bidAmt) FROM Bid bb WHERE bb.lastUpdtTs BETWEEN ?1 AND ?2)
答案 2 :(得分:0)
你第二次得到的错误是
多重身份证明 变量[b],先前声明为 [竞标b]
从上面的错误中,似乎多次声明了别名,因此从子查询中删除别名b
尝试以下查询
SELECT b FROM Bid b WHERE b.bidAmt =
(SELECT MAX(bidAmt) FROM Bid WHERE lastUpdtTs BETWEEN ?1 AND ?2)