案例一
将a表中的指定字段插入b表中:
insert into b (id,name,address) select id,name, "北京" from a
案例二
将a表中的name字段按连接符连接后写入b表,(a表中多行数据合并成b表中的一行数据)
insert into b(id, name,address)
(SELECT id,(select GROUP_CONCAT(distinct name SEPARATOR '|') as tmp_type FROM a WHERE id in(1,2,3))
FROM a WHERE id=1
#distinct 用来对字段进行去重
#同样类似的函数还有CONCAT、 CONCAT_ws
案例三
按addr字段分组后取得组内条数是2的记录,并且合并id和n_type字段,同时只取其中一条数据的id做为新记录的id.
最终效果便是分组内的两条数据合并成一条
select addr, GROUP_CONCAT(distinct id SEPARATOR '|') as
sum_ids,SUBSTRING_INDEX(GROUP_CONCAT(distinct id SEPARATOR
'|'),'|',1) as tmp_id,GROUP_CONCAT(distinct n_type
SEPARATOR '|') as tmp_type from val_t GROUP BY
addr HAVING count(1)=2