【发布时间】:2014-11-15 09:04:52
【问题描述】:
我在 Postgres 9.3 数据库中有一个表,其中包含这样的 json 列:
CREATE mytable (
mycolumn json
)
我想从如下所示的 Java 应用程序执行查询:
SELECT
mycolumn->>'somefield',
count(*)
FROM
mytable
GROUP BY
mycolumn->>'somefield'
当我尝试像这样使用 PreparedStatement 时:
SELECT
mycolumn->>?,
count(*)
FROM
mytable
GROUP BY
mycolumn->>?
我收到以下错误:
PSQLException: ERROR: column "mytable.mycolumn" must appear in the GROUP BY clause or be used in an aggregate function
这是有道理的,因为 Postgres 不能保证两个位置参数相同。
使用 psql,我可以编写如下语句:
PREPARE mystatement AS
SELECT
mycolumn->>$1,
count(*)
FROM
mytable
GROUP BY
mycolumn->>$1
用 JDBC 可以做到这一点吗?
【问题讨论】:
标签: postgresql jdbc