0

I have two tables tblA and tblX. In tblA, there is a foreign key FKX from tblX. I want to copy corresponding values of StringColumn to strColCopy column. It introduces redundancy but it's part of longer migrations process.

How can I access tblX.StringColumn cell for every single tblA.FKX cell?

enter image description here

1
  • Which dbms are you using? Commented Mar 30, 2018 at 9:24

1 Answer 1

1

You can use UPDATE using JOIN

Solution for SQL Server:

UPDATE A SET A.strCol = X.StringColumn FROM TblA A JOIN (SELECT FKX, strCol FROM TxlX GROUP BY FKX, strCol) AS X ON A.FKX = X.ID 

Syntax for general Update join for SQL Server:

UPDATE a SET a.columnToUpdate = [something] FROM TABLEA a JOIN TABLEB b ON a.colA = b.colB 

Solution for MySQL:

UPDATE TblA A JOIN (SELECT FKX ,strCol FROM TxlX GROUP BY FKX ,strCol)AS X ON A.FKX = X.ID SET A.strCol = X.StringColumn 

Syntax for general Update join for MySQL:

UPDATE TABLEA a JOIN TABLEB b ON a.colA = b.colB SET a.columnToUpdate = [something] 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.