Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In SQL how do you update each row of the table by finding all rows that are equal for a column, then set another column equal to eachother

So basically this would be the psuedo code, but I don't know how to do this in SQL, please help.

for each row in table1{
    loop through each row in table 2 {
        if table1's row.column 1 = table2's row.column 2 for this row {
            set table1's row.col2 = table2's row.col2
        }
    }
}

Edit: Okay let me be more specific. We are basically switching from hibernate sequence as ids to using guids for the id column. I'm trying to update the foreign keys associated by making a temp of the previous foreign key column and then matching the temporary columns to update the actual columns.

suppose table one had id's and table two had a column for those ids to use as foreign keys. I wanna use the previous values in table 2 to match with the rows in table 1 and set the key values in table 2 to match the new guid of table 1.

so table 2 may have multiple rows of duplicate id's but table 1 will never have duplicates. If that makes any sense.

like image 498
hthomos Avatar asked Jan 08 '14 15:01

hthomos


People also ask

How do I update multiple rows in SQL with update?

There are a couple of ways to do it. INSERT INTO students (id, score1, score2) VALUES (1, 5, 8), (2, 10, 8), (3, 8, 3), (4, 10, 7) ON DUPLICATE KEY UPDATE score1 = VALUES(score1), score2 = VALUES(score2);

How do you update to all row of a table in SQL?

Syntax: UPDATE table_name SET column_name1 = new_value1, column_name2 = new_value2 ---- WHERE condition; Here table_name is the name of the table, column_name is the column whose value you want to update, new_value is the updated value, WHERE is used to filter for specific data.

How do you update a column based on another column in SQL?

UPDATE table SET col = new_value WHERE col = old_value AND other_col = some_other_value; UPDATE table SET col = new_value WHERE col = old_value OR other_col = some_other_value; As you can see, you can expand the WHERE clause as much as you'd like in order to filter down the rows for updating to what you need.

How can I update multiple rows of a single column in SQL?

First, specify the table name that you want to change data in the UPDATE clause. Second, assign a new value for the column that you want to update. In case you want to update data in multiple columns, each column = value pair is separated by a comma (,). Third, specify which rows you want to update in the WHERE clause.


1 Answers

In SQL Server you can do something like:

UPDATE Table_1
SET Column_2 = t2.Column_2
FROM Table_1 AS t1
INNER JOIN Table_2 AS t2 ON t2.Column_1 = t1.Column_1

or something like

UPDATE Table_1
SET Column_2 = ( 
    SELECT t2.Column_2
    FROM Table_2 AS t2
    WHERE t2.Column_1 = Table_1.Column_1
)

Of course if you have multiple rows in Table_2, you will get an error....

like image 93
Laurent PerrucheJ Avatar answered Nov 07 '22 10:11

Laurent PerrucheJ