aakashgupta.0205
aakashgupta.0205

Reputation: 687

Updating values in SQL Table based on same values in two columns

I want to update values in tables that for any set of data in two columns, the third column is same. For example

Column1|  Column2|  Column3|
       |         |         |
      1|        2|        3|
      2|        1|        4|
      5|        6|        7|

After the query the result output should be

Column1|  Column2|  Column3|
       |         |         |
      1|        2|        3|
      2|        1|        3|
      5|        6|        7|     

as in values (1, 2) and (2, 1) in column1 and column2 should have same values for column3

Upvotes: 0

Views: 122

Answers (1)

tsnorri
tsnorri

Reputation: 2097

Use a self-join:

UPDATE your_table t1 -- Your table goes here
SET [Column3] = rand()
FROM your_table t2 -- and here
WHERE (t1.[Column1] = t2.[Column1] AND t1.[Column2] = t2.[Column2]) OR
    (t1.[Column1] = t2.[Column2] AND t1.[Column2] = t2.[Column1])

Upvotes: 1

Related Questions