MySQL 中的 CONCAT() 有無替代品?


是的,CONCAT_WS() 是一個替代品。我們先建立一個表 −

mysql> create table DemoTable
   (
   StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   StudentName varchar(100)
   );
Query OK, 0 rows affected (0.74 sec)

示例

使用插入命令在表中插入一些記錄 −

mysql> insert into DemoTable(StudentName) values('Chris');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable(StudentName) values('Robert');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable(StudentName) values('Bob');
Query OK, 1 row affected (0.12 sec)

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

mysql> select *from DemoTable;

輸出

+-----------+-------------+
| StudentId | StudentName |
+-----------+-------------+
| 1         | Chris       |
| 2         | Robert      |
| 3         | Bob         |
+-----------+-------------+
3 rows in set (0.00 sec)

我們來看看如何在 MySQL 中使用 CONCAT() 的替代品 −

mysql> select concat_ws(SPACE(2), 'Student Name is:',StudentName) from DemoTable;

輸出

+-----------------------------------------------------+
| concat_ws(SPACE(2), 'Student Name is:',StudentName) |
+-----------------------------------------------------+
| Student Name is: Chris                              |
| Student Name is: Robert                             |
| Student Name is: Bob                                |
+-----------------------------------------------------+
3 rows in set (0.04 sec)

更新於: 2-Jul-2020

733 人瀏覽

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.