括号中的SELECT命令用于显示行,如何更新表?

时间:2017-08-04 11:04:36

标签: sql postgresql

UPDATE tab4mail SET emid = 'X' WHERE
(

SELECT tab1.colum1, tab1.colum2, tab1.status, tab2.idnr, tab2.role, tab3.colum2, tab4mail.emid

FROM tab1 JOIN tab2 ON tab1.colum1 = tab2.column9

JOIN tab3 ON (tab1.colum1 = tab3.colum1 and tab1.colum2 = tab3.colum2)

JOIN tab4mail ON tab2.idnr = tab4mail.idnr

WHERE tab1.status = 'X' and tab2.role = 'S'

);

括号中的SELECT命令用于显示行。

现在我想更新表“tab2mail”并想要设置为“X”。我不知道在哪里写/添加此UPDATE。在第一行或其他地方?我只想在“tab4mail”中替换满足SELECT命令条件的这些行,用括号括起来。

2 个答案:

答案 0 :(得分:0)

我认为你想要一个相关的子查询:

UPDATE tab4mail
    SET emid = 'X'
    WHERE EXISTS (SELECT 1
                  FROM tab1 JOIN
                       tab2
                       ON tab1.colum1 = tab2.column9 JOIN
                       tab3
                       ON tab1.colum1 = tab3.colum1 and tab1.colum2 = tab3.colum2
                  WHERE tab2.idnr = tab4mail.idnr AND tab1.status = 'X' and tab2.role = 'S'
                 );

答案 1 :(得分:0)

您想使用IN子句。

UPDATE tab4mail 
SET emid = 'X' 
WHERE idnr IN (
        SELECT tab4mail.idnr 
        FROM tab1 JOIN tab2 ON tab1.colum1 = tab2.column9
        JOIN tab3 ON (tab1.colum1 = tab3.colum1 and tab1.colum2 = tab3.colum2)
        JOIN tab4mail ON tab2.idnr = tab4mail.idnr
        WHERE tab1.status = 'X' and tab2.role = 'S'
    )