Search code examples
mysqlsql-updatesql-like

Add text string to existing mySQL table


I would like to be able to mass add a certain text string to a DB table's row.

Example table look like this:

description d -id -name -tag

If d.name has LIKE %text% add 'Tag text' to d.tag.

But d.tag will have data (text) and I would like to add the new text to it.


Solution

  • You can use the function CONCAT to append text to d.tag like this:

    UPDATE description d
    SET d.tag = CONCAT(d.tag, 'Tag text')
    WHERE d.name LIKE '%text%';
    

    EDIT: Solution for the first comment (but please see my comment on why this is a bad way to solve the problem):

    UPDATE description d
    SET d.tag = CONCAT(d.tag, 'Tag text')
    WHERE d.name LIKE '%text%' and d.tag = '';
    UPDATE description d
    SET d.tag = CONCAT(d.tag, ', Tag text')
    WHERE d.name LIKE '%text%' and d.tag != '';