我有3个阵列。例如:
let status = [1,2,3];
let name = ['Andrey','Vasya','Petia'];
let age = [23,45,54];
此外,我还为每个要更新的用户提供了一系列ID。
let id_list = [2323,3434,3434]
我想发送postgres请求,我以这种方式通过一个请求更新此数据:
UPDATE users SET status = '1' , name = 'Andrey', age = '23' WHERE id ='2323'
UPDATE users SET status = '2' , name = 'Vasya', age = '45' WHERE id ='3434'
等。
我想在一个请求中更新的所有数据
答案 0 :(得分:2)
首先,你必须取消你的阵列:
WITH sample (id, name, status, age) AS (
SELECT
*
FROM
--Using unnest function
unnest(
ARRAY[2323, 3434, 3434],
ARRAY['Andrey','Vasya','Petia'],
ARRAY[1,2,3],
ARRAY[23,45,54]
)
)
--And then proceed to your update
UPDATE
users
SET status = s.status , name = s.name, age = s.age
FROM sample s
WHERE users.id = s.id;
有关unnest
函数here的更多信息。