Postgres Error: More than one row returned by a subquery used as an expression

后端 未结 6 1355
无人共我
无人共我 2020-12-01 09:24

I have two separate databases. I am trying to update a column in one database to the values of a column from the other database:

UPDATE customer
SET customer         


        
6条回答
  •  盖世英雄少女心
    2020-12-01 09:43

    The fundamental problem can often be simply solved by changing an = to IN, in cases where you've got a one-to-many relationship. For example, if you wanted to update or delete a bunch of accounts for a given customer:

    WITH accounts_to_delete AS 
        ( 
            SELECT     account_id
            FROM       accounts a
            INNER JOIN customers c
                    ON a.customer_id = c.id
            WHERE      c.customer_name='Some Customer'
        )
    
    -- this fails if "Some Customer" has multiple accounts, but works if there's 1:
    DELETE FROM accounts
     WHERE accounts.guid = 
    ( 
        SELECT account_id 
        FROM   accounts_to_delete 
    );
    
    -- this succeeds with any number of accounts:
    DELETE FROM accounts
     WHERE accounts.guid IN   
    ( 
        SELECT account_id 
        FROM   accounts_to_delete 
    );
    

提交回复
热议问题