在 MySQL 中如何將字串與數字連線?


若要將字串與數字連線,請使用 CONCAT() 方法。首先讓我們建立一個表 −

mysql> create table DemoTable682(
   Name varchar(100),
   Age int
);
Query OK, 0 rows affected (0.49 sec)

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

mysql> insert into DemoTable682 values('John',23);
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable682 values('Chris',21);
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable682 values('David',25);
Query OK, 1 row affected (0.17 sec)
Display all records from the table using select statement:

使用 select 語句從表中顯示所有記錄 -

mysql> select *from DemoTable682;

這將產生以下輸出 −

+-------+------+
| Name  | Age  |
+-------+------+
| John  |   23 |
| Chris |   21 |
| David |   25 |
+-------+------+
3 rows in set (0.00 sec)

以下是實現 concat() 方法以連線記錄的查詢 −

mysql> select concat(Name,Age) from DemoTable682;

這將產生以下輸出 −

+------------------+
| concat(Name,Age) |
+------------------+
| John23           |
| Chris21          |
| David25          |
+------------------+
3 rows in set (0.00 sec)

更新於:2019-08-26

1K+ 檢視

開啟你的 職業生涯

學習完成課程並獲取證書

開始
廣告
© . All rights reserved.