明细表1字符串拼接合并插入到明细表2SQL输出过程记录

【明细表1字符串拼接合并插入到明细表2SQL输出过程记录】冲天香阵透长安,满城尽带黄金甲。这篇文章主要讲述明细表1字符串拼接合并插入到明细表2SQL输出过程记录相关的知识,希望能为你提供帮助。
需求描述:明细表1:formtable_main_46_dt1
字段:id,mainid, bxmx1, jshj, kmbm, fyxm
明细表4:formtable_main_46_dt4
字段:id,mainid,zy,kmbm,jdbmbm,fyxmbm,yfxmbm,jfje

  1. fyxm 为福利费(56)的数据。
  2. bxmx1 拼接,”/”间隔(mysql 结果集行拼接group_concat)。
  3. jshj 取合计(sum)。
  4. 插入到明细表4.
  5. 同主表,所以mainid相同。
输出过程:

  1. 查看明细表1数据。
  2. select mainid, bxmx1, jshj, kmbm, fyxm from formtable_main_46_dt1;
  3. 找一个demo数据,mainid=62,以及过滤符合条件的数据,fyxm=56
  4. select mainid, bxmx1, jshj, kmbm, fyxm from formtable_main_46_dt1 where mainid=62 and fyxm =56
  5. 尝试字符串合并group_concat
  6. select group_concat(bxmx1) from formtable_main_46_dt1 where mainid=62 and fyxm =56
  7. 逗号替换为‘/’
  8. select REPLACE(group_concat(bxmx1),,,/) as zy from formtable_main_46_dt1 where mainid=62 and fyxm =56
  9. 金额求和
  10. select REPLACE(group_concat(bxmx1),,,/) as zy ,sum(jshj) as jfje from formtable_main_46_dt1 where mainid=62 and fyxm =56
  11. 添加其它字段
  12. select mainid,REPLACE(group_concat(bxmx1),,,/) as zy ,kmbm,sum(jshj) as jfje from formtable_main_46_dt1 where mainid=62 and fyxm =56
  13. 提高兼容性,加group by
  14. select mainid,REPLACE(group_concat(bxmx1),,,/) as zy ,kmbm,sum(jshj) as jfje from formtable_main_46_dt1 where mainid=62 and fyxm =56 group by fyxm
  15. 改为insert into 语句前补全字段
  16. select mainid,REPLACE(group_concat(bxmx1),,,/) as zy,kmbm,null as jdbmbm,null as fyxmbm,null as yfxmbm,sum(jshj) as jfje from formtable_main_46_dt1 where mainid=62 and fyxm =56 group by fyxm;
  17. 改为insert into 语句(测试需谨慎)
  18. INSERT INTO formtable_main_46_dt4 ( mainid,zy,kmbm,jdbmbm,fyxmbm,yfxmbm,jfje) select mainid,REPLACE(group_concat(bxmx1),,,/) as zy,kmbm,null as jdbmbm,null as fyxmbm,null as yfxmbm,sum(jshj) as jfje from formtable_main_46_dt1 where mainid=62 and fyxm =56 group by fyxm;





    推荐阅读