Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update an entire single column with table joined query [duplicate]

I have a table that I just added a column to and now I am trying to find an easy way to update it.

select * from KioskGoals kg
inner join [TestDB].dbo.Kiosks k on kg.kioskID = k.Id

The joined table has the names I need. I want to update KioskGoals table and set the kioskName column = to the name returned from [TestDB].dbo.Kiosks which would be k.name

Is this possible with a single query?

The table that needs to be updated is KioskGoals. The column that needs to be updated is kioskName.

like image 843
James Wilson Avatar asked Jun 09 '14 16:06

James Wilson


People also ask

How do you avoid duplicates when joining tables?

Solution. Select column values in a specific order within rows to make rows with duplicate sets of values identical. Then you can use SELECT DISTINCT to remove duplicates. Alternatively, retrieve rows in such a way that near-duplicates are not even selected.

Why join query returns duplicate rows?

Unwanted rows in the result set may come from incomplete ON conditions. In some cases, you need to join tables by multiple columns. In these situations, if you use only one pair of columns, it results in duplicate rows.

Does join allow duplicates in SQL?

The answer is yes, if there are any. If there are duplicate keys in the tables being joined.


1 Answers

UPDATE kg
 SET Kg.kioskName =  K.name
from KioskGoals kg inner join [TestDB].dbo.Kiosks k 
on kg.kioskID = k.Id
like image 64
M.Ali Avatar answered Nov 10 '22 14:11

M.Ali