【问题标题】:How to set alias to columns retrieved with jsonb_to_recordset?如何为使用 jsonb_to_recordset 检索的列设置别名?
【发布时间】:2020-11-08 12:14:32
【问题描述】:

我有包含对象数组的“电话”列。例如

[
   {
     "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 覆盖了初始电话列。如何为连接列设置别名,使其不会覆盖原始列?

【问题讨论】:

    标签: sql postgresql eloquent


    【解决方案1】:

    你可以给他们新的称呼:

    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
    

    【讨论】:

    • 非常感谢!我还想问一下,是否可以在我的横向连接中使用“where”语句?所以它只会加入 country_code 具有我需要的值或 null 的记录(此时它加入数组中的每个对象)
    • @rusyko 。 . .过滤应该没问题。我怀疑在过滤掉的行上调用了该函数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-27
    • 1970-01-01
    • 1970-01-01
    • 2018-02-10
    • 1970-01-01
    相关资源
    最近更新 更多