id type intro
1   1    aaa
2   3    bbb
3   2    ccc
4   3    ddd
5   2    eee
6   2    fff
......
-------------------
结果:
aaa
bbb
ccc,eee,fff
ddd如果type=2则合并成一条结果,请教sql写法

解决方案 »

  1.   

    select type,group_concat(intro)
    from tb
    group by type
      

  2.   

    是MYSQL?
    select `type`,group_concat(intro) from tt group by `type`
      

  3.   

    假设表名称为t2SELECT DISTINCT * FROM (
    SELECT
      IF(`type`=2,(SELECT GROUP_CONCAT(intro) FROM t2 WHERE `type`=2),intro)
    FROM t2) a
      

  4.   

    select group_concat(intro)
    from table1
    group by if(type=2,0,id)
      

  5.   

    mysql> select * from t_diyle ;
    +------+------+-------+
    | id   | type | intro |
    +------+------+-------+
    |    1 |    1 | aaa   |
    |    2 |    3 | bbb   |
    |    3 |    2 | ccc   |
    |    4 |    3 | ddd   |
    |    5 |    2 | eee   |
    |    6 |    2 | fff   |
    +------+------+-------+
    6 rows in set (0.00 sec)mysql> select group_concat(intro)
        -> from t_diyle
        -> group by if(type=2,0,id);
    +---------------------+
    | group_concat(intro) |
    +---------------------+
    | ccc,eee,fff         |
    | aaa                 |
    | bbb                 |
    | ddd                 |
    +---------------------+
    4 rows in set (0.01 sec)mysql>