使用 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)

更新时间: 2019-7-30

645 浏览量

提升你的 职业

完成课程以获得认证

入门
广告
© . All rights reserved.