提问人:blueSky 提问时间:11/14/2023 最后编辑:blueSky 更新时间:11/14/2023 访问量:76
java.sql.SQLSyntaxErrorException:意外标记:ON
java.sql.SQLSyntaxErrorException: unexpected token: ON
问:
我有这个查询,它在 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
P.S. 使用 distinct on 的原因是我需要返回table1.profile_id但需要按table3.creation_date排序。 如果我将从 Java 代码中获得的查询复制/粘贴到数据库客户端中,它仍然运行而不会出错。
答:
-1赞
G10
11/14/2023
#1
on 是保留字,你不能我们作为别名。如果要使用包装的 '' 符号on
评论
0赞
Maurice Perry
11/14/2023
它在哪里用作别名?
评论
select distinct on ...
distinct on