如何为使用jsonn_to_recordset检索到的列设置别名



我有列"电话";包含对象数组。例如

[
{
"id": 8789789789,
"phone": "111111111",
"default": true,
"code": "11",
"country_code": "US"
}
]

我需要连接这个数组中某个对象的一些值,所以我使用带有交叉连接横向的jsonb_to_recordset来连接这个

select * 
from "phones" cross join lateral 
jsonb_to_recordset(phone) as r(phone jsonb, country_code jsonb)
where "r.country_code" = ? and "deleted_at" is null

这里的问题是,检索到的电话(从对象(用json覆盖了最初的电话列。如何为联接的列设置别名,使它们不会覆盖原始列?

你可以给他们新的别名:

select p.*,
r.phone as r_phone,
r.r_country_code as r_country_code
from "phones" p cross join lateral 
jsonb_to_recordset(phone) as r(phone jsonb, country_code jsonb)
where "country_code" = ? and "deleted_at" is null

最新更新