使用 CONCAT 函数在 MySQL 列中添加现有值?

mysqlmysqli database更新于 2025/3/31 1:48:17

为了理解这个概念,让我们首先创建一个演示表。

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)

使用 insert 命令在表中插入一些记录。 查询语句如下 −

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)


相关文章


有用资源