SQL update where value in other table is - sql

I need to update a value in a table if a specific value exists in another table.
i.e.
update table1
set value1=3
where table2.value2='Y'
There is a key ref1 in both tables- how do i use this key to link these together? Many thanks!

update table1
inner join table2 on table1.ref1 = table2.ref1
set value1 = 3
where table2.value2 = 'Y'

you can write a join between both tables and then make the update from the join.
Something like:
update tableA
set column = b.value
from tableA a
join tableB on a.key = b.key

Related

How do I update the duplicate values and their foreign key in the child table?

I have 2 tables
Table A
Table B
Table A has 2 columns
ID, CellNo
Now Table B is the CHILD table of A.
In Table B, we have
ID, TableA_CellNO, TableA_ID.
I have duplicate CellNo's in table A and in Table B those two records exist but TableA_ID is updated with the wrong id's.
I need to Update all of the TableA_ID's with the correct values against the duplicate CellNo.
I have written a query to update by matching CellNo in both table but that doesn't work.
UPDATE TableA AS R
INNER JOIN TableB AS P
ON R.CellNo = P.TableA_CellNo
SET P.TableA_ID = R.ID
I'm assuming you want to update table A's Id to table B's latest Id, meaning that the last CellNo record inserted into B is the correct one.
This may not be the most efficient but it should work.
UPDATE a
SET a.Id = b.Id
FROM TableA a
JOIN TableB b
ON b.Id = a.Id
AND b.Id =
(
SELECT MAX(ib.Id) FROM TableB ib WHERE ib.CellNo = a.CellNo
);
You 've a syntax error there. I suppose you 're looking for
UPDATE P
SET P.TableA_ID = R.ID
FROM TableA R
JOIN TableB P ON R.CellNo = P.CellNo;
--WHERE <Some Conditions> if needed

How to copy values corresponding to the foreign key from another table to this table?

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?
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]

SQL copy values of column a of table A into column b of table B

I need to copy the values of column a of table A into column b of Table B.
Is this statement correct?
UPDATE
TableA,
TableB
SET
TableB.b = TableA.a
WHERE
TableA.Id = TableB.Id
If the column of the destination is empty and you do not need to match something just use this
INSERT INTO DestinationTb ([ColumnName])
SELECT [ColumnNameToTransfer] FROM [SourceTable]
if not just with a join
update A
SET A.Columnname = B.ColumnNameToTransfer
from DestinationTb A
INNER JOIN
SourceTable B
ON
--HERE ADD YOUR MATCHING FOR EXAMPLE
A.ID = B.ID;
or with a subquery
UPDATE DestinationTb
SET ColumnName = (
SELECT ColumnNameToTransfer
FROM SourceTable
--HERE ADD YOUR MATCHING FOR EXAMPLE
WHERE SourceTable.id = DestinationTb.id
);
One solution is to use FROM and JOIN behind UPDATE
UPDATE A
SET A.a= B.b
FROM TableA A
JOIN TableB B ON A.ID = B.ID

How do I lookup a value in a table and insert lookup result in the same table?

I created a table from a CSV file. Although the table has all the data we could ever need for this project, the resulting table is not normalized. My task is to clean and normalize this table.
Given a field value Value1 from TableA, how do I lookup that value in TableB and place the PK from TableB in a FK column in TableA?
Same as Question 1 exception I need to lookup multiple columns. Given field values Value1 and Value2 from TableA, how do I lookup that combination in TableB and place the PK From TableB in a FK column in TableA?
This should work for #1:
UPDATE TableA
SET FKColumn = b.PKColumn
FROM TableA as a
INNER JOIN TableB as b
ON a.Value1 = b.Value1
For #2:
UPDATE TableA
SET FKColumn = b.PKColumn
FROM TableA as a
INNER JOIN TableB as b
ON a.Value1 = b.Value1 AND a.Value2 = b.Value2
UPDATE TableA SET FKColumn = TableB.PKColumn
FROM TableB
WHERE TableA.Value1 = TableB.Value1
UPDATE TableA SET FKColumn = TableB.PKColumn
FROM TableB
WHERE TableA.Value1 = TableB.Value1 AND TableA.Value2 = TableB.Value2
UPDATE TableA a
SET a.fk_col = (select b.pk_col
from TableB b
where b.value = a.value);
And
UPDATE TableA a
SET a.fk_col = (select b.pk_col
from TableB b
where b.value = a.value1
and b.value = a.value2);
Logically, it should be something like this:
UPDATE TableA
SET FK_col =
(SELECT PK_col
FROM TableB
WHERE TableB.TableA_field = TableA.field)
WHERE <your filter criteria on TableA>
However, at least in MySQL, you can't directly reference TableA in sub-queries like this in some circumstances, but this can be worked around in different ways depending upon the circumstances. I'm not sure if the same applies to SQL Server or not - never touched it.
Edit: I think the other answers posted in the meantime are probably better than mine! ;)

refer to outside field value in subselect?

I want to do a query to update values that I forgot to copy over in a mass insert. However I'm not sure how to phrase it.
UPDATE table
SET text_field_1 = (SELECT text_field_2
FROM other_table
WHERE id = **current row in update statement, outside parens**.id )
How do I do this? It seems like a job for recursion.
Use:
UPDATE YOUR_TABLE
SET text_field_1 = (SELECT t.text_field_2
FROM other_table t
WHERE t.id = YOUR_TABLE.id)
Warning
If there's no supporting record in other_table, text_field_1 will be set to NULL.
Explanation
In standard SQL, you can't have table aliases on the table defined for the UPDATE (or DELETE) statement, so you need to use full table name to indicate the source of the column.
It's called a correlated subquery -- the correlation is be cause of the evaluation against the table from the outer query.
Clarification
MySQL (and SQL Server) support table aliases in UPDATE and DELETE statement, in addition to JOIN syntax:
UPDATE YOUR_TABLE a
JOIN OTHER_TABLE b ON b.id = a.id
SET a.text_field_1 = b.text_field_2
...is not identical to the provided query, because only the rows that match will be updated -- those that don't match, their text_field_1 values will remain untouched. This is equivalent to the provided query:
UPDATE YOUR_TABLE a
LEFT JOIN OTHER_TABLE b ON b.id = a.id
SET a.text_field_1 = b.text_field_2
If there is one ID field:
UPDATE updtable t1
SET t1.text_field_1 = (
SELECT t2.text_field_2
FROM seltable t2
WHERE t1.ID = t2.ID
)
;
UPDATE Table1, Tabl2
SET Table1.myField = Table2.SomeField
WHERE Table1.ID = Table2.ID
Note: I have not tried it.
This will only update records where IDs match.
Try this:
UPDATE table
SET text_field_1 = (SELECT text_field_2
FROM other_table
WHERE id = table.id )