UPDATE 2 columns of a table of values from a table

Steven Yong

I have these 2 tables

Table P

A     | B    | C     | D       
-----------------------------
'abc' | NULL | 'kkk' | NULL
'xyz' | NULL | 'www' | NULL
'pqr' | NULL | 'ccc' | NULL


Table Q

X     | Y
-----------------------------
'abc' | 123
'xyz' | 12
'pqr' | 34

I need to update Table P column B and D with the value of Y from Table Q as long as the column A and C in Table P matches values of column X in Table Q.

My UPDATE now:

UPDATE TableP 
SET B = Q.Y
FROM TableQ Q (NOLOCK)
WHERE Q.X = A

UPDATE TableP 
SET D = Q.Y
FROM TableQ Q (NOLOCK)
WHERE Q.X = C

I only know how to do it in 2 UPDATE statements, how do I do this in a single UPDATE statement?

Lukasz Szozda

You could use UPDATE FROM JOIN:

UPDATE p
SET B = q1.Y,        --COALESCE(q1.Y, p.B) if needed
    D = q2.Y         --COALESCE(q2.Y, p.D)
FROM TableP p
LEFT JOIN TableQ q1
  ON p.A = q1.X
LEFT JOIN TableQ q2
  ON p.C = q2.X;

LiveDemo

Output:

╔═════╦═════╦═════╦═══╗
║  A  ║  B  ║  C  ║ D ║
╠═════╬═════╬═════╬═══╣
║ abc ║ 123 ║ kkk ║   ║
║ xyz ║  12 ║ www ║   ║
║ pqr ║  34 ║ ccc ║   ║
╚═════╩═════╩═════╩═══╝

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Update multiple columns of a table using aggregate values from another table

From Dev

Update multiple columns of a table using aggregate values from another table

From Dev

update table with values from 2 other tables

From Dev

SQL - Update table1 values from table2

From Dev

SQLite - Update a column based on values from another table's columns

From Dev

How to update multiple columns based on values from an associated table?

From Dev

Update table with values from other table

From Dev

MySQL - Update table values from another table

From Dev

Update a table with values from a related table

From Dev

Update table with values from other table

From Dev

Update a table based on values from a other table

From Dev

MYSQL Update one table with the values other table qualified using 2 columns

From Dev

Update columns with equal values in the same table

From Dev

Update multiple columns in one table based on values in another table in mysql

From Dev

Return Values from Table 1 based on multiple columns from table 2

From Dev

Sqlite: Update multiple columns from a table

From Dev

Sqlite: Update multiple columns from a table

From Dev

Update Query From 2 Table

From Dev

SQL - Update table values from values in another table

From Dev

update values of sql table using the values from the same table

From Dev

Compare 2 table columns and update new column

From Dev

Mysql JOIN table and update 2 columns information

From Dev

Mysql JOIN table and update 2 columns information

From Dev

Select all columns with distinct values from a table

From Dev

Updating columns values from another table SQL

From Dev

Select all columns with distinct values from a table

From Dev

Merging two columns with values from another table

From Dev

update value from a table if from other two table values match

From Dev

How to update a table with 2 random values, check in and check out dates/columns

Related Related

  1. 1

    Update multiple columns of a table using aggregate values from another table

  2. 2

    Update multiple columns of a table using aggregate values from another table

  3. 3

    update table with values from 2 other tables

  4. 4

    SQL - Update table1 values from table2

  5. 5

    SQLite - Update a column based on values from another table's columns

  6. 6

    How to update multiple columns based on values from an associated table?

  7. 7

    Update table with values from other table

  8. 8

    MySQL - Update table values from another table

  9. 9

    Update a table with values from a related table

  10. 10

    Update table with values from other table

  11. 11

    Update a table based on values from a other table

  12. 12

    MYSQL Update one table with the values other table qualified using 2 columns

  13. 13

    Update columns with equal values in the same table

  14. 14

    Update multiple columns in one table based on values in another table in mysql

  15. 15

    Return Values from Table 1 based on multiple columns from table 2

  16. 16

    Sqlite: Update multiple columns from a table

  17. 17

    Sqlite: Update multiple columns from a table

  18. 18

    Update Query From 2 Table

  19. 19

    SQL - Update table values from values in another table

  20. 20

    update values of sql table using the values from the same table

  21. 21

    Compare 2 table columns and update new column

  22. 22

    Mysql JOIN table and update 2 columns information

  23. 23

    Mysql JOIN table and update 2 columns information

  24. 24

    Select all columns with distinct values from a table

  25. 25

    Updating columns values from another table SQL

  26. 26

    Select all columns with distinct values from a table

  27. 27

    Merging two columns with values from another table

  28. 28

    update value from a table if from other two table values match

  29. 29

    How to update a table with 2 random values, check in and check out dates/columns

HotTag

Archive