How does MySQL merge a single field of multiple records to one record? I found it online today using the following method:
Test Table Structure:
----------------------------------------------------------
--
-- Table structure 'tet'
--
Create Table if not exists 'tet '(
'Id' int (11) not null,
'Name' varchar (255) not null,
'Url' varchar (255) not null
) Engine = InnoDB default charset = utf8;
--
-- Store the data in the table 'tet'
--
Insert into 'tet' ('id', 'name', 'url') Values
(1, 'Baidu ', 'HTTP: // www.baidu.com '),
(0, 'Google ', 'HTTP: // www.google.com.hk '),
(3, 'phone 400 ', 'HTTP: // www.my400800.cn ');
Method 1:
Select group_concat (name) Name
From Tet
Where 1 = 1
Limit 0, 30
Result:
Name: Baidu, Google, 400 phone number
Group_concat can also be used
The keyword separator specifies the connector. The SQL statement is as follows:
Select group_concat (URL separator "@") URL
From Tet
Where 1 = 1
Limit 0, 30
Result:
Http://www.baidu.com @ http://www.google.com.hk @ http://www.my400800.cn
Method 2:
Select group_concat (name) Name
From Tet
Where 1 = 1
Group by ID
Limit 0, 30
Result: