ashim
ashim

Reputation: 25560

MySQL, how update string field by concatenating to it?

Question is how to update string field by concatenating to it?

Here is code for creating database and adding one row:

CREATE DATABASE  my_db;                                                          
USE my_db;

CREATE TABLE IF NOT EXISTS my_table(
     article_id INTEGER unsigned,
     revision_ids VARCHAR(10),
     PRIMARY KEY (article_id)
);

INSERT INTO my_table (article_id, revision_ids) VALUES (1, "154");

I need to write code which concatenate string to revision_ids field. For example, I need to concatenate ", 999" to "154", so I will get "154, 999". My version of code does not work:

UPDATE my_table SET revision_ids = CONCAT((SELECT revision_ids FROM my_table WHERE article_id = 1), ", ", "999") WHERE article_id = 1;

How to do it?

There is one important condition. Theoretically this concatenation could do several scripts, so it is important that while our updating nobody can change the field value.

Upvotes: 3

Views: 16808

Answers (3)

xdazz
xdazz

Reputation: 160833

Use:

UPDATE my_table SET revision_ids = CONCAT(revision_ids, ", 999") WHERE article_id = 1;

Upvotes: 2

Kermit
Kermit

Reputation: 34055

This should be all you need:

UPDATE my_table SET revision_ids = CONCAT(revision_ids, ", ", "999") WHERE article_id = 1;

Upvotes: 6

John Woo
John Woo

Reputation: 263693

I gues you can do this directly,

UPDATE my_table 
SET revision_ids = CONCAT(revision_ids, ', ', '999') 
WHERE article_id = 1;

Upvotes: 2

Related Questions