MySQL中同一字段值对应的两个不同的值进行连接

在MySQL中有一张表,结构如下:

表table1

ListId, CustomerId, TitleId
1          101               201
2          101               202
3          102               203
4          102               201

相同的CustomerId最多只有两个不同的TitleId值,要得到如下结果
1          101               201,202
2          102               203,201

使用语句:

select a.customerid, CONCAT(a.titleid,',',IFNULL(b.titleid,'')) as titleid from table1 a
left join table1 b
on a.listid<>b.listid and a.customerid=b.customerid

posted @ 2008-10-23 10:11  一沙  阅读(3862)  评论(0)    收藏  举报