用GROUP_CONCAT()试试,如果不行用临时表

解决方案 »

  1.   

    http://community.csdn.net/Expert/TopicView3.asp?id=4200633
    查到了这个帖子,果然可以,哈哈,谢谢mysql> create table tb(id char(2),aid char(2),value char(2));
    Query OK, 0 rows affected (0.08 sec)mysql> insert into ta value('00','0a'),('01','0b'),('02','0c'),('03','0d');
    Query OK, 4 rows affected (0.03 sec)
    Records: 4  Duplicates: 0  Warnings: 0mysql> insert into tb value('b0','00','b0'),('b1','00','b1'),('b2','01','b2'),('
    b3','03','b3'),('b4','03','b4');
    Query OK, 5 rows affected (0.03 sec)
    Records: 5  Duplicates: 0  Warnings: 0mysql> select ta.id,ta.name,group_concat(tb.id order by tb.value separator ',')
    as tbid,
        -> group_concat(tb.value order by tb.value separator ',') as tbvalue from ta
    ,tb where tb.aid=
        -> ta.id group by ta.id;
    +------+------+-------+---------+
    | id   | name | tbid  | tbvalue |
    +------+------+-------+---------+
    | 00   | 0a   | b0,b1 | b0,b1   |
    | 01   | 0b   | b2    | b2      |
    | 03   | 0d   | b3,b4 | b3,b4   |
    +------+------+-------+---------+
    3 rows in set (0.00 sec)mysql>
      

  2.   

    现在还有个问题:
    我想让tbid和tbvalue两列只显示前5条记录,这个应该怎么实现呢?
      

  3.   

    好像没法取前几个,不能用limit还有就是用临时表,不知道在数据量大的时候哪个好一些
      

  4.   

    我想的是有没有办法可以限制group_concat取得字符串的长度?或者可以截断也行啊
      

  5.   

    那可以
    left(group_concat(tb.id order by tb.value separator ','),***)
      

  6.   

    搞定了,十分感谢:XqYuan() !!!!!!