使用 CONCAT 函数向现有 MySQL 列值添加值?
为了理解这个概念,我们首先创建一个演示表。
mysql> create table addToExistingValueDemo -> ( -> Instructor_Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Instructor_Name varchar(30), -> Instructor_TechnicalSubject text -> ); Query OK, 0 rows affected (0.54 sec)
使用插入命令向表中插入一些记录。查询如下所示 -
mysql> insert into addToExistingValueDemo(Instructor_Name,Instructor_TechnicalSubject) values('John','C,C++');
Query OK, 1 row affected (0.15 sec)
mysql> insert into addToExistingValueDemo(Instructor_Name,Instructor_TechnicalSubject) values('Carol','Java,Python');
Query OK, 1 row affected (0.18 sec)
mysql> insert into addToExistingValueDemo(Instructor_Name,Instructor_TechnicalSubject) values('Bob','MySQL,SQL Server');
Query OK, 1 row affected (0.15 sec)
mysql> insert into addToExistingValueDemo(Instructor_Name,Instructor_TechnicalSubject) values('David','DataStructure');
Query OK, 1 row affected (0.18 sec使用 select 语句显示表中的所有记录。查询如下所示 -
mysql> select *from addToExistingValueDemo;
输出如下
+---------------+-----------------+-----------------------------+ | Instructor_Id | Instructor_Name | Instructor_TechnicalSubject | +---------------+-----------------+-----------------------------+ | 1 | John | C,C++ | | 2 | Carol | Java,Python | | 3 | Bob | MySQL,SQL Server | | 4 | David | DataStructure | +---------------+-----------------+-----------------------------+ 4 rows in set (0.00 sec)
以下是如何使用 CONCAT 函数向 MySQL 列中添加到现有值内容的查询
mysql> update addToExistingValueDemo -> set Instructor_TechnicalSubject=concat(Instructor_TechnicalSubject,', Introduction To Algorithm') -> where Instructor_Id=4; Query OK, 1 row affected (0.10 sec) Rows matched: 1 Changed: 1 Warnings: 0
让我们再次检查表记录以查看新更改。查询如下所示 -
mysql> select *from addToExistingValueDemo;
输出如下
+---------------+-----------------+------------------------------------------+ | Instructor_Id | Instructor_Name | Instructor_TechnicalSubject | +---------------+-----------------+------------------------------------------+ | 1 | John | C,C++ | | 2 | Carol | Java,Python | | 3 | Bob | MySQL,SQL Server | | 4 | David | DataStructure, Introduction To Algorithm | +---------------+-----------------+------------------------------------------+ 4 rows in set (0.00 sec)
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程语言
C++
C#
MongoDB
MySQL
Javascript
PHP