0

I have two tables in a database and I wish to copy text from one column of the table to another column in another table.

Lets say I have two tables called 'articles' and 'info'. I have to copy articles.article column text into info.description for the same articles, which can be identified by a so called 'Codename' and not ID. Preferably IF the column info.description is empty. I intend to use WHERE category = 4 to limit myself to only some articles, but that shouldn't be a problem and if the IF statement is correct even unnecessary.

Tables layout (Not whole, but enough for here):

Articles (ID, Codename (VARCHAR), Article (VARCHAR)) 
Info (ID2, Codename (VARCHAR), Description (VARCHAR))

So basically, I would like to give articles with no description a basic description with their name and some data, that is written beside the name.

2 Answers 2

1

Use a UPDATE FROM syntax:

UPDATE Info
SET Description = (Select Article
                   FROM Articles 
                   WHERE Info.Codename = Articles.Codename)
WHERE Description IS NULL;

http://rextester.com/EYIG7377

0
2

Although previous answer was correct, and I am thankful to @McNets, I found an easier way to do it.

UPDATE info i, articles a
SET i.Description = a.Article
WHERE i.Codename = a.Codename
AND i.Description IS NULL;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.