如有表
id      字段1      字段2   字段N1 4 1 …
2 4 2 …
3 4 3 …
4 4 4 …
5 4 5 …
6 4 6 …
7 5 7 …
8 5 8 …
9 5 9 …
10 5 10 …
11 5 11 …
12 5 12 …
13 5 13 …
14 5 14 …
15 6 15 …
16 6 16 …
17 6 17 …
18 6 18 …
19 6 19 …
20 6 20 …
如果  字段1  相等,例如。 上面的。。字段1都等于4,那么把他们的相应的ID的项。。的字段2都连接起来。如
id      字段1      字段2   字段N1 4 1 …
2 4 2 …
3 4 3 …
4 4 4 …
5 4 5 …
6 4 6 …变成id      字段1      字段2   字段N1 4 123456 …
2 4 123456 …
3 4 123456 …
4 4 123456 …
5 4 123456 …
6 4 123456 …
全部则换成id      字段1      字段2   字段N1 4 1 …
2 4 2 …
3 4 3 …
4 4 4 …
5 4 5 …
6 4 6 …
7 5 7 …
8 5 8 …
9 5 9 …
10 5 10 …
11 5 11 …
12 5 12 …
13 5 13 …
14 5 14 …
15 6 15 …
16 6 16 …
17 6 17 …
18 6 18 …
19 6 19 …
20 6 20 …
全部则换成id      字段1      字段2   字段N1 4 123456 …
2 4 123456 …
3 4 123456 …
4 4 123456 …
5 4 123456 …
6 4 123456 …
7 5 78910111211314 …
8 5 78910111211314 …
9 5 78910111211314 …
10 5 78910111211314 …
11 5 78910111211314 …
12 5 78910111211314 …
13 5 78910111211314 …
14 5 78910111211314 …
15 6 151617181920 …
16 6 151617181920 …
17 6 151617181920 …
18 6 151617181920 …
19 6 151617181920 …
20 6 151617181920 …

解决方案 »

  1.   

    select a.*,b.new_list from tb_name a inner join (select 字段1,group_concat(字段2) as new_list from tb_name) b on a.字段1=b.字段1
      

  2.   

    mysql> select * from t_fengchujun;
    +------+------+------+
    | id   | col1 | col2 |
    +------+------+------+
    |    1 |    4 | 1    |
    |    2 |    4 | 2    |
    |    3 |    4 | 3    |
    |    4 |    4 | 4    |
    |    5 |    4 | 5    |
    |    6 |    4 | 6    |
    |    7 |    5 | 7    |
    |    8 |    5 | 8    |
    |    9 |    5 | 9    |
    |   10 |    5 | 10   |
    |   11 |    5 | 11   |
    |   12 |    5 | 12   |
    |   13 |    5 | 13   |
    |   14 |    5 | 14   |
    |   15 |    6 | 15   |
    |   16 |    6 | 16   |
    |   17 |    6 | 17   |
    |   18 |    6 | 18   |
    |   19 |    6 | 19   |
    |   20 |    6 | 20   |
    +------+------+------+
    20 rows in set (0.00 sec)mysql> update t_fengchujun a ,(
        ->  select col1 ,group_concat(col2 SEPARATOR '') as gcol2
        ->  from t_fengchujun
        ->  group by col1
        -> ) b
        -> set a.col2=b.gcol2
        -> where a.col1=b.col1;
    Query OK, 20 rows affected (0.11 sec)
    Rows matched: 20  Changed: 20  Warnings: 0mysql> select * from t_fengchujun;
    +------+------+---------------+
    | id   | col1 | col2          |
    +------+------+---------------+
    |    1 |    4 | 165432        |
    |    2 |    4 | 165432        |
    |    3 |    4 | 165432        |
    |    4 |    4 | 165432        |
    |    5 |    4 | 165432        |
    |    6 |    4 | 165432        |
    |    7 |    5 | 1413121110987 |
    |    8 |    5 | 1413121110987 |
    |    9 |    5 | 1413121110987 |
    |   10 |    5 | 1413121110987 |
    |   11 |    5 | 1413121110987 |
    |   12 |    5 | 1413121110987 |
    |   13 |    5 | 1413121110987 |
    |   14 |    5 | 1413121110987 |
    |   15 |    6 | 191817161520  |
    |   16 |    6 | 191817161520  |
    |   17 |    6 | 191817161520  |
    |   18 |    6 | 191817161520  |
    |   19 |    6 | 191817161520  |
    |   20 |    6 | 191817161520  |
    +------+------+---------------+
    20 rows in set (0.00 sec)mysql>
      

  3.   

    group_concat有长度限制。。怎么办
      

  4.   


    group_concat_max_len SET [GLOBAL | SESSION] group_concat_max_len = val;