java.sql.SQLSyntaxErrorException:意外标记:ON

问题描述 投票:0回答:1

我有这个查询在 DBeaver for Postgres 上运行良好:

select profileId from (
    select distinct on (p.profile_id) p.profile_id as profileId, pr.creation_date as creationDate
    from table1 p
    
             join table2 o on (p.profile_id = o.offeree_profile_id) 
             join table3 pr on (o.offer_id = pr.offer_id ) 
    where 
     
             o.offer_status_id = 'ACCEPTED' 
             
    and (pr.status != 'TERMINATED') 
             and o.offeror_profile_id in 
    
    (select p.profile_id from table4 u 
    join table1 p on (p.user_id = u.user_id) 
    where customer_id = 'A'
    AND o.offeror_profile_id != o.offeree_profile_id)
    ) sub
order by creationDate desc limit 6

但是当我从 Java 代码运行它时,它会抛出错误:

String 
queryString = "select profileId from (" +
    "select distinct on (p.profile_id) p.profile_id as profileId, pr.creation_date as creationDate " +
    "from table1 p " +
    "join table2  o on (p.profile_id = o.offeree_profile_id ) " +
    "join table3 pr on (o.offer_id = pr.offer_id ) " +
    "where " +
    "o.offer_status_id = 'ACCEPTED' " +
    "and (pr.status != 'TERMINATED') " +
    "and o.offeror_profile_id in " +
    "(select p.profile_id from table4 u " +
    "join table1 p on (p.user_id = u.user_id) " +
    "where customer_id = :CustomerId " +
    "and o.offeror_profile_id != o.offeree_profile_id)" +
    ") sub " +
    "order by creationDate desc limit 6";


final Query profileIdQuery = getCurrentSession().createSQLQuery(queryString).setString("CustomerId", CustomerId);

List<BigInteger> profileIdList = profileIdQuery.list();

上面的行抛出此错误:

Caused by: java.sql.SQLSyntaxErrorException: unexpected token: ON

附注使用distinct on的原因是我需要返回table1.profile_id,但需要按table3.creation_date排序。 如果我将从 Java 代码获得的查询复制/粘贴到数据库客户端中,它仍然可以正常运行。

java sql postgresql dbeaver sqlexception
1个回答
0
投票

on 是保留字,不能用作别名。如果你想使用包装的'

on
'

© www.soinside.com 2019 - 2024. All rights reserved.