日期:2014-05-16  浏览次数:20785 次

【转】mysql同一表记录某一字段内容合并

mysql> create table t(id int,name varchar(20));

Query OK, 0 rows affected (0.09 sec)

?

mysql> insert into t values (1,'aa'),(1,'bb'),(2,'cc'),(2,'dd');

Query OK, 4 rows affected (0.00 sec)

Records: 4 ?Duplicates: 0 ?Warnings: 0

?

mysql> commit;

Query OK, 0 rows affected (0.00 sec)

?

mysql> select * from t;

+------+------+

| id ? | name |

+------+------+

| ? ?1 | aa ? |

| ? ?1 | bb ? |

| ? ?2 | cc ? |

| ? ?2 | dd ? |

+------+------+

4 rows in set (0.01 sec)

?

mysql> select id,GROUP_CONCAT(name) from t group by id;

+------+--------------------+

| id ? | GROUP_CONCAT(name) |

+------+--------------------+

| ? ?1 | aa,bb ? ? ? ? ? ? ?|

| ? ?2 | cc,dd ? ? ? ? ? ? ?|

+------+--------------------+

2 rows in set (0.00 sec)

?

转自:http://blog.csdn.net/gulang76/article/details/5589479

1 楼 maoghj 20 小时前