更新行时出现问题。我在一个表中有“count”行,并引用了另一个表“population_id”,希望使用select更新此行“count”,select返回值为(count(client_id)、population_id)的us表
有办法做到这一点,最简单的方法是什么?
CREATE OR REPLACE FUNCTION zonning.set_count_population(label text)
RETURNS void AS
$BODY$declare
BEGIN
UPDATE zonning.populations2 SET count =
(SELECT count(c.client_id)
FROM clients.clients c
JOIN clients.phone_numbers pn ON c.client_id = pn.client_id
JOIN clients.phone_numbers_labels pnl ON pn.phone_number_id = pnl.phone_number_id
JOIN clients.labels l ON pnl.label_id = l.label_id
WHERE l.label_name = label)
WHERE population_id = "How i can get id from select?";
END;$BODY$ `
LANGUAGE plpgsql VOLATILE
COST 100;
最佳答案
在PostgreSQL中,可以在FROM
语句中使用UPDATE
子句与其他表联接,如下所示:
UPDATE populations2 pn
SET count = (subselect using "pn" and "ot")
FROM other_table ot
WHERE pn.population_id = ot.id;
关于mysql - 从选择中更新返回我们表的行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39929792/