0

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.

1
  • It would be useful to supply the SQL query you used to create the table, add data to it, or any attempts you've made. Your table definition is a little confusing. Have you looked up documentation for MySQL UPDATE? Commented Nov 22, 2015 at 22:14

1 Answer 1

2

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 != '';
Sign up to request clarification or add additional context in comments.

2 Comments

This work fine! But, let's say I want to have multiple values separated by comma (,) in d.tag I have tried to add ...CONCAT(d.tag,",","Tag text")... If d.tag is empty the data will then be ",Tag text". Can the comma(,) be skipped somehow it d.tag is empty?
First off, let me say that this is not a good way to solve your problem. You should normalize your database (see the example and read up on first normal form). In your case, you should add a table tags with the columns description_id and tagText. I've edited my answer to address your question, but again, the proper way to solve this is to normalize your table.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.