First, create a sample table
CREATE TABLE testtable (name VARCHAR (ten), skill varchar), insert into TestTable VALUES (' Zhang San ', ' php '); INSERT INTO Testtab Le values (' Zhang San ', ' Java '), insert into testtable values (' Zhang San ', ' C # '), insert into testtable values (' John Doe ', ' C '); INSERT INTO Te Sttable VALUES (' John Doe ', ' C + + ');
The table structure is:
| Name |
Skill |
| Tom |
Php |
| Tom |
Java |
| Tom |
C# |
| John doe |
C |
| John doe |
C++ |
The result of the desired query:
| Name |
Allskill |
| Tom |
php,java,c# |
| John doe |
C,c++ |
Second, implement in SQL Server
--SQL Serverselect Name,stuff ( ( SELECT ', ' + skill from testtable a WHERE a.name = b.name for XML PA TH (") ), 1, 1, ') as Allskillfrom testtable bgroup by name
Third, the implementation of MySQL
--Mysqlselect Name,group_concat (skill) as Allskillfrom testtablegroup by name;
IV. implementation in Oracle
--Oracleselect Name,wmsys. Wm_concat (skill) as Allskillfrom testtablegroup by name;
The database queries n rows to 1 rows