更新select中返回us表的行

时间:2016-10-08 07:17:15

标签: mysql sql postgresql select

我有更新行的问题。我在一个表中有行“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;

2 个答案:

答案 0 :(得分:0)

我不是MySQL专家,但我认为应该可以这样做:

FromEventPattern

以下是简化示例:

<强> sqlfiddle demo

答案 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;