SQL update from one Table to another based on a ID match

后端 未结 22 1584
太阳男子
太阳男子 2020-11-21 22:49

I have a database with account numbers and card numbers. I match these to a file to update any card numbers to the account number, so

22条回答
  •  轮回少年
    2020-11-21 23:21

    This will allow you to update a table based on the column value not being found in another table.

        UPDATE table1 SET table1.column = 'some_new_val' WHERE table1.id IN (
                SELECT * 
                FROM (
                        SELECT table1.id
                        FROM  table1 
                        LEFT JOIN table2 ON ( table2.column = table1.column ) 
                        WHERE table1.column = 'some_expected_val'
                        AND table12.column IS NULL
                ) AS Xalias
        )
    

    This will update a table based on the column value being found in both tables.

        UPDATE table1 SET table1.column = 'some_new_val' WHERE table1.id IN (
                SELECT * 
                FROM (
                        SELECT table1.id
                        FROM  table1 
                        JOIN table2 ON ( table2.column = table1.column ) 
                        WHERE table1.column = 'some_expected_val'
                ) AS Xalias
        )
    

提交回复
热议问题