MySQL – How to Copy Text from One Table to Another If Target Column Is Empty

copyinsertMySQLtable

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.

Best Answer

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