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 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写法
from tb
group by type
select `type`,group_concat(intro) from tt group by `type`
SELECT
IF(`type`=2,(SELECT GROUP_CONCAT(intro) FROM t2 WHERE `type`=2),intro)
FROM t2) a
from table1
group by if(type=2,0,id)
+------+------+-------+
| 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>