MySQL,如何通过串联来更新字符串字段? [英] MySQL, how update string field by concatenating to it?

查看:95
本文介绍了MySQL,如何通过串联来更新字符串字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是如何通过串联来更新字符串字段?

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");

我需要编写将字符串连接到version_ids字段的代码.例如,我需要将,999"连接到"154",所以我将得到"154,999".我的代码版本不起作用:

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;

该怎么做?

有一个重要条件.从理论上讲,此串联可以执行多个脚本,因此重要的是,在我们进行更新时,任何人都不能更改字段值.

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.

推荐答案

这应该是您所需要的:

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

这篇关于MySQL,如何通过串联来更新字符串字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆