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