Andrei Vlad Andrei Vlad - 6 months ago 22
MySQL Question

Update sql row without deleting data

is it possible to do such thing?

UPDATE news SET article_title="["+id+"]" + article_title;


Is it possible to process this query for every row in the news table?
I want to add the id of the article to the article title like this:
[1000] StackOverFlow

Kind regards!

Answer

Yes it is:

UPDATE news SET article_title=CONCAT('[',id,'] ',article_title);

You could also add a trigger on your table news to it do this update automatically. It would be something like this:

CREATE TRIGGER trg_news 
   BEFORE INSERT ON news
   FOR EACH ROW
   BEGIN
       NEW.article_title = CONCAT('[',id,'] ', NEW.article_title);
   END;

Note that this trigger would work only on an INSERT command. That is because for an update you would have to parse the article_title so you don't end up with something like: [1] someName[1] someName