Postgres - jsonb:使用从另一个 table 获取的值更新列中的键
Postgres - jsonb : Update key in column with value taken from another table
我正在使用 postgres 9.5。我有一个配置文件 table,其中列出了姓名:
public.profiles:
id | first_name | last_name
--- --------------- ---------------------
1 Jason Bourne
2 Jhonny Quest
我有一张发票table:
public.invoices:
invoice_id | billing_address | profile_id
------------------ ----------------------------- ---------------------
1 { 2
"address_line1": "445 Mount
Eden Road",
"city":"Mount Eden",
"country": "Auckland"
}
我想用配置文件 table 中的 first_name 和 last_name 更新发票 table 的 billing_address 列,例如:
public.invoices:
invoice_id | billing_address | profile_id
------------------ ----------------------------- ---------------------
1 {
"name" : "Jhonny Quest" 2
"address_line1": "445 Mount
Eden Road",
"city":"Mount Eden",
"country": "Auckland"
}
为此,我尝试使用 jsonb_set:
UPDATE invoices AS i SET billing_address = jsonb_set(billing_address,'{name}', SELECT t::jsonb FROM (SELECT CONCAT (p.first_name,p.middle_name, p.last_name) FROM profiles p WHERE p.id = i.profile_id)t )
它在 SELECT 处抛出错误。 TBH 我什至不确定该声明是否合法。寻找任何指导。
UPDATE invoices i
SET billing_address = s.new_billing_address
FROM (
SELECT
i.invoice_id,
jsonb_set(
billing_address,
'{name}'::text[],
to_jsonb(concat_ws(' ', first_name, last_name))
) AS new_billing_address
FROM
invoices i
JOIN profiles p ON i.profile_id = p.id
) s
WHERE s.invoice_id = i.invoice_id;
创建 SELECT
并加入第二个 table;之后,您可以使用 to_jsonb()
和连接运算符 ||
(或 concat_ws(),当然,如评论)。
我正在使用 postgres 9.5。我有一个配置文件 table,其中列出了姓名:
public.profiles:
id | first_name | last_name
--- --------------- ---------------------
1 Jason Bourne
2 Jhonny Quest
我有一张发票table:
public.invoices:
invoice_id | billing_address | profile_id
------------------ ----------------------------- ---------------------
1 { 2
"address_line1": "445 Mount
Eden Road",
"city":"Mount Eden",
"country": "Auckland"
}
我想用配置文件 table 中的 first_name 和 last_name 更新发票 table 的 billing_address 列,例如:
public.invoices:
invoice_id | billing_address | profile_id
------------------ ----------------------------- ---------------------
1 {
"name" : "Jhonny Quest" 2
"address_line1": "445 Mount
Eden Road",
"city":"Mount Eden",
"country": "Auckland"
}
为此,我尝试使用 jsonb_set:
UPDATE invoices AS i SET billing_address = jsonb_set(billing_address,'{name}', SELECT t::jsonb FROM (SELECT CONCAT (p.first_name,p.middle_name, p.last_name) FROM profiles p WHERE p.id = i.profile_id)t )
它在 SELECT 处抛出错误。 TBH 我什至不确定该声明是否合法。寻找任何指导。
UPDATE invoices i
SET billing_address = s.new_billing_address
FROM (
SELECT
i.invoice_id,
jsonb_set(
billing_address,
'{name}'::text[],
to_jsonb(concat_ws(' ', first_name, last_name))
) AS new_billing_address
FROM
invoices i
JOIN profiles p ON i.profile_id = p.id
) s
WHERE s.invoice_id = i.invoice_id;
创建 SELECT
并加入第二个 table;之后,您可以使用 to_jsonb()
和连接运算符 ||
(或 concat_ws(),当然,如评论)。