在 MySQL 中連線兩個字串並留出空格?


為此,可以使用 MySQL 中的 concat() 函式。我們首先建立一個表 -

mysql> create table DemoTable
   -> (
   -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> Name varchar(20),
   -> Subject varchar(100)
   -> );
Query OK, 0 rows affected (17.73 sec)

使用 insert 命令向表中插入一些記錄 -

mysql> insert into DemoTable(Name,Subject) values('John','MySQL');
Query OK, 1 row affected (1.19 sec)

mysql> insert into DemoTable(Name,Subject) values('Chris','SQL Server');
Query OK, 1 row affected (0.88 sec)

mysql> insert into DemoTable(Name,Subject) values('Robert','MongoDB');
Query OK, 1 row affected (2.62 sec)

使用 select 語句顯示錶中的所有記錄 -

mysql> select *from DemoTable;

輸出

+----+--------+------------+
| Id | Name   | Subject    |
+----+--------+------------+
| 1  | John   | MySQL      |
| 2  | Chris  | SQL Server |
| 3  | Robert | MongoDB    |
+----+--------+------------+
3 rows in set (0.00 sec)

以下是將兩個字串連線在一起並留出空格的查詢 -

mysql> update DemoTable
   -> set Subject=concat(Name,' ',Subject);
Query OK, 3 rows affected (0.38 sec)
Rows matched: 3 Changed: 3 Warnings: 0

讓我們檢查表中的所有記錄 -

mysql> select *from DemoTable;

輸出

+----+--------+------------------+
| Id | Name   | Subject          |
+----+--------+------------------+
| 1  | John   | John MySQL       |
| 2  | Chris  | Chris SQL Server |
| 3  | Robert | Robert MongoDB   |
+----+--------+------------------+
3 rows in set (0.00 sec)

更新於: 2019 年 7 月 30 日

1K+ 瀏覽量

啟動您的 職業

完成課程獲取認證

開始
廣告
© . All rights reserved.