这个Mysql语句的等效pg SQL是什么(UPDATE语句中的内连接)?

时间:2017-06-06 12:41:38

标签: postgresql

MySQL的:

$content = $response->getOriginalContent()->getData()['content']

Postgresql(我知道):

UPDATE a INNER JOIN b on a.b_id = b.id SET n=1 WHERE b.n > 2

但是对于以下内容的等效pg语句是什么:

UPDATE a SET n=1 FROM b WHERE b.n > 2 AND a.b_id = b.id

UPDATE a OUTER JOIN b on a.b_id = b.id SET n=1 WHERE b.n > 2

更一般地说,如果我在Mysql中有几个内连接表(例如3个表),那么等效的pg语句是什么:

UPDATE a LEFT JOIN b on a.b_id = b.id SET n=1 WHERE b.n > 2

2 个答案:

答案 0 :(得分:2)

通常,您可以创建这样的子查询(非常灵活和清晰):

UPDATE  tblA
SET     colA = subQuery.colA
FROM    (
        SELECT  tblA.id, tblA.colA
        FROM    tblA
                INNER JOIN tblB AS b    ON b.id = tblA.b_id
                INNER JOIN tblC AS c    ON c.id = b.c_id
        WHERE   c.someData > 100
        ) AS subQuery
WHERE   tblA.id = subQuery.id

答案 1 :(得分:1)

如果您使用n>进行过滤,则使用左连接是什么? 2?

Table a:
id | firstname | b_id
1  | elisabeth | 2
2  | sam       | 2
3  | john      | 3

table b:
id | surname
2  | smith
3  | doe

UPDATE a LEFT JOIN b on a.b_id = b.id SET firstname = null WHERE b.id > 2

只有john doe会更新。

至于这一个:

UPDATE a 
INNER JOIN b on a.b_id = b.id 
INNER JOIN c on b.c_id = c.id 
INNER JOIN d on c.d_id = d.id 
SET n=1 WHERE d.n > 2

在postgres中:

UPDATE a
SET n=1
FROM b, c, d
WHERE a.b_id = b.id
AND b.c_id = c.id
AND c.d_id = d.id
AND d.n > 2