- 论坛徽章:
- 0
|
以下是我在csdn看到的一个方法。
mysql4.1及以上版本用group_concat函数实现
mysql> create table t1(no int not null,name varchar(10));
Query OK, 0 rows affected (0.51 sec)
mysql> insert into t1 values
-> (1,'a'),(1,'b'),(1,'c'),(1,'d'),(2,'a'),(2,'b'),(2,'c'),(3,'d');
Query OK, 8 rows affected (0.13 sec)
Records: 8 Duplicates: 0 Warnings: 0
mysql> select * from t1;
+----+------+
| no | name |
+----+------+
| 1 | a |
| 1 | b |
| 1 | c |
| 1 | d |
| 2 | a |
| 2 | b |
| 2 | c |
| 3 | d |
+----+------+
8 rows in set (0.16 sec)
mysql> select no,group_concat(name separator ',') from t1 group by no;
+----+----------------------------------+
| no | group_concat(name separator ' ') |
+----+----------------------------------+
| 1 | a,b,c,d |
| 2 | a,b,c |
| 3 | d |
+----+----------------------------------+
3 rows in set (1.36 sec) |
|