小编Кир*_*кий的帖子

Postgres 不能将类型 jsonb 转换为整数

有两张桌子。

表格1

 id integer
 color_name character(64) 
Run Code Online (Sandbox Code Playgroud)

表2

 id integer
 jdata jsonb
Run Code Online (Sandbox Code Playgroud)

Json 数据如下所示:

{"price": 4500, "colorId": 5}
Run Code Online (Sandbox Code Playgroud)

我需要输出颜色和按颜色分组的项目数,所以我尝试使用这个查询:

SELECT Table1.color_name, Table2.jdata ->> 'colorId', count(*)
FROM Table1
INNER JOIN Table2
ON Table1.id = Table2.jdata ->> 'colorId'
group by Table2.jdata ->> 'colorId';
Run Code Online (Sandbox Code Playgroud)

我收到一个错误:

错误:运算符不存在:整数 = jsonb

我也试过执行这个:

select Table1.color_name, count(*) 
from Table1
join Table2
on (Table2.jdata->>'colorId')::int = Table1.id
group by Table1.color_name
Run Code Online (Sandbox Code Playgroud)

我得到了什么:

错误:无法将类型 jsonb 转换为整数

postgresql json join

6
推荐指数
2
解决办法
1万
查看次数

标签 统计

join ×1

json ×1

postgresql ×1