Postgresql用连接更新表

时间:2016-10-27 14:18:21

标签: sql postgresql join

我需要使用其他表中的信息更新表。 这是我的SQL查询:

UPDATE service a 
JOIN agency b USING (feed_id) 
SET end_date = (SELECT MIN(start_date)-1 
                FROM service c 
                JOIN agency d USING (feed_id) 
                WHERE b.feed_id = a.feed_+1 
                      AND b.agency_id = d.agency_id)

我需要在条件中使用代理商ID,因此唯一的方法是将两个表与代理商表联系起来。

Postgres回归:

ERROR:  syntax error at or near "JOIN"
LINE 1: UPDATE service a JOIN agency b USING (feed_id) SET end_date ...

任何提示?

2 个答案:

答案 0 :(得分:1)

试试这个:)

UPDATE service
SET end_date = (SELECT MIN(start_date)-1 
                FROM service c 
                JOIN agency d USING (feed_id) 
                WHERE b.feed_id = a.feed_+1 
                      AND b.agency_id = d.agency_id)
FROM service a
JOIN agency b USING (feed_id) 

答案 1 :(得分:0)

此查询有效:

UPDATE service a
SET end_date = (SELECT MIN(start_date)-1
            FROM service b
            JOIN agency c ON b.feed_id = c.feed_id
            JOIN agency d ON a.feed_id = d.feed_id 
            WHERE b.feed_id = a.feed_id+1 
            AND c.agency_id = d.agency_id
            )