我在查詢中使用 json_recordset 語法,看起來像這樣
jsonb_to_recordset(t.field_42) as field_42(message text, messageid numeric,tablerowid numeric, addedon timestamp)
我正在嘗試查看是否有一種方法可以為此處參考的欄位設定別名?例如,記錄集中的“addon”欄位......有沒有辦法將其別名為不同的名稱,例如“startaddeon”?
我試圖把“as”放在這樣的語法中:
jsonb_to_recordset(t.field_42) as field_42(message text, messageid numeric,tablerowid numeric, addedon timestamp as 'startaddedon')
但它不喜歡那種語法。
uj5u.com熱心網友回復:
這是一個插圖。
with the_table(id, field_42) as
(
values
(1, '[
{"message":"one", "messageid":1, "tablerowid":101, "addedon":"2022-10-06 22:20"},
{"message":"two", "messageid":2, "tablerowid":102, "addedon":"2022-10-06 22:21"}
]'::jsonb),
(2, '[
{"message":"three","messageid":3, "tablerowid":103, "addedon":"2022-10-06 22:22"},
{"message":"four", "messageid":4, "tablerowid":104, "addedon":"2022-10-06 22:23"}
]')
)
select t.id, l.message, l.messageid, l.tablerowid,
l.addedon as startaddedon -- here it is
from the_table t
cross join lateral jsonb_to_recordset(t.field_42)
as l(message text, messageid numeric,tablerowid numeric, addedon timestamp);
| ID | 資訊 | 訊息ID | tablerowid | 開始添加 |
|---|---|---|---|---|
| 1 | 一 | 1 | 101 | 2022-10-06 22:20:00.0 |
| 1 | 二 | 2 | 102 | 2022-10-06 22:21:00.0 |
| 2 | 三 | 3 | 103 | 2022-10-06 22:22:00.0 |
| 2 | 四 | 4 | 104 | 2022-10-06 22:23:00.0 |
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/511368.html
標籤:PostgreSQL
