如何使用 MySQL group_concat 引用值?
您可以使用 MySQL 中的 concat() 和 grop_concat() 函式引用值。語法如下 -
SELECT GROUP_CONCAT(CONCAT(' '' ', yourColumnName, ' '' ' )) as anyVariableName from yourTableName;為了理解上述語法,讓我們建立一個表。建立表的查詢如下 -
mysql> create table Group_ConcatDemo -> ( -> Id int NOT NULL AUTO_INCREMENT, -> Value int, -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (1.56 sec)
現在,您可以使用 insert 命令在表中插入一些記錄。查詢如下 -
mysql> insert into Group_ConcatDemo(Value) values(100); Query OK, 1 row affected (0.24 sec) mysql> insert into Group_ConcatDemo(Value) values(120); Query OK, 1 row affected (0.22 sec) mysql> insert into Group_ConcatDemo(Value) values(234); Query OK, 1 row affected (0.11 sec) mysql> insert into Group_ConcatDemo(Value) values(2345); Query OK, 1 row affected (0.24 sec) mysql> insert into Group_ConcatDemo(Value) values(5678); Query OK, 1 row affected (0.14 sec) mysql> insert into Group_ConcatDemo(Value) values(86879); Query OK, 1 row affected (0.16 sec)
使用 select 語句顯示錶中的所有記錄。查詢如下 -
mysql> select *from Group_ConcatDemo;
以下是輸出 -
+----+-------+ | Id | Value | +----+-------+ | 1 | 100 | | 2 | 120 | | 3 | 234 | | 4 | 2345 | | 5 | 5678 | | 6 | 86879 | +----+-------+ 6 rows in set (0.00 sec)
以下是使用 group_concat() 引用值 的查詢 -
mysql> select GROUP_CONCAT(CONCAT('''', Value, '''' )) as SingleQuote from Group_ConcatDemo;以下是輸出 -
+-----------------------------------------+ | SingleQuote | +-----------------------------------------+ | '100','120','234','2345','5678','86879' | +-----------------------------------------+ 1 row in set (0.09 sec)
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP