我有更新行的问题。我在一个表中有行“count”并引用另一个表“population_id”,并且希望使用select返回这个行“count”,其中返回带有值的表(count(client_id),population_id) 有办法做到这一点,最简单的方法是什么?
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;
答案 0 :(得分:0)
答案 1 :(得分:0)
在PostgreSQL中,您可以在FROM
语句中使用UPDATE
子句与其他表连接,如下所示:
UPDATE populations2 pn
SET count = (subselect using "pn" and "ot")
FROM other_table ot
WHERE pn.population_id = ot.id;