將多個引數值插入到 MySQL 的單個列中?


要將多個引數值插入到單個列中,請使用 CONCAT_WS() 或 CONCAT()。我們先建立一個表 -

mysql> create table DemoTable
(
   Name varchar(100),
   Age int,
   CountryName varchar(100),
   PersonInformation text
);
Query OK, 0 rows affected (0.67 sec)

以下是將多個引數值插入到單個列中的查詢。我們將使用與用於在 MySQL 表中插入記錄的 INSERT 命令相同的命令 -

mysql> insert into DemoTable values('John',21,'US',concat_ws('-',Name,Age,CountryName));
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values('Chris',22,'AUS',concat_ws('-',Name,Age,CountryName));
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values('Bob',24,'UK',concat_ws('-',Name,Age,CountryName));
Query OK, 1 row affected (0.17 sec)

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

mysql> select *from DemoTable;

這將產生以下輸出 -

+-------+------+-------------+-------------------+
| Name  | Age  | CountryName | PersonInformation |
+-------+------+-------------+-------------------+
| John  | 21   | US          | John-21-US        |
| Chris | 22   | AUS         | Chris-22-AUS      |
| Bob   | 24   | UK          | Bob-24-UK         |
+-------+------+-------------+-------------------+
3 rows in set (0.00 sec)

更新於: 01 -十月 - 2019

401 次瀏覽

開啟您的 職業生涯

修完課程即可獲得認證

入門
廣告
© . All rights reserved.