mysql SUBSTRING_INDEX 一种行列转换的思想

本文深入探讨了MySQL中的SUBSTRING_INDEX函数,解析其工作原理,并展示了如何利用该函数进行数据的行列转换,从而优化数据处理和查询效率。通过对实例的分析,读者将更好地理解和运用这一实用的数据库操作技巧。
摘要由CSDN通过智能技术生成

-- --  行列转换
 SELECT  a.verify_no,a.relation_ids, SUBSTRING_INDEX(SUBSTRING_INDEX(a.relation_ids,',',b.id),',',-1) AS resultNAME  
 FROM fc_corms_cost_verify a  JOIN  fc_add_canopy  b  ON b.id < (LENGTH(a.relation_ids) - LENGTH(REPLACE(a.relation_ids,',',''))+2)
 where a.relation_ids like '%,%' and a.pay_no_arr is null ;
-- 
-- -- 去重
-- SELECT fccps.cost_pay_id,fccps.id, mm.verify_no,fccp.payment_document from 
-- fc_corms_cost_pay_site as fccps,
-- (SELECT  a.verify_no,a.relation_ids, SUBSTRING_INDEX(SUBSTRING_INDEX(a.relation_ids,',',b.id),',',-1) AS resultNAME  FROM fc_corms_cost_verify a  JOIN  fc_add_canopy  b  ON b.id < (LENGTH(a.relation_ids) - LENGTH(REPLACE(a.relation_ids,',',''))+2)where a.relation_ids like '%,%' and a.pay_no_arr is null ) 
-- as mm ,
-- fc_corms_cost_pay as fccp 
-- where fccps.id=mm.resultNAME and fccps.cost_pay_id = fccp.id GROUP BY  mm.verify_no,fccp.payment_document;
-- 
-- 
-- -- 拼接结果集
-- SELECT kk.verify_no,GROUP_CONCAT(kk.payment_document)
--   from (SELECT fccps.cost_pay_id,fccps.id, mm.verify_no,fccp.payment_document from 
-- fc_corms_cost_pay_site as fccps,
-- (SELECT  a.verify_no,a.relation_ids, SUBSTRING_INDEX(SUBSTRING_INDEX(a.relation_ids,',',b.id),',',-1) AS resultNAME  FROM fc_corms_cost_verify a  JOIN  fc_add_canopy  b  ON b.id < (LENGTH(a.relation_ids) - LENGTH(REPLACE(a.relation_ids,',',''))+2)where a.relation_ids like '%,%' and a.pay_no_arr is null ) 
-- as mm ,
-- fc_corms_cost_pay as fccp 
-- where fccps.id=mm.resultNAME and fccps.cost_pay_id = fccp.id GROUP BY  mm.verify_no,fccp.payment_document) as kk GROUP BY kk.verify_no;
-- 
-- 
-- -- charu
-- INSERT INTO test_ref_temp (verify_code, payno_arry)
-- SELECT kk.verify_no,GROUP_CONCAT(kk.payment_document)
--   from (SELECT fccps.cost_pay_id,fccps.id, mm.verify_no,fccp.payment_document from 
-- fc_corms_cost_pay_site as fccps,
-- (SELECT  a.verify_no,a.relation_ids, SUBSTRING_INDEX(SUBSTRING_INDEX(a.relation_ids,',',b.id),',',-1) AS resultNAME  FROM fc_corms_cost_verify a  JOIN  fc_add_canopy  b  ON b.id < (LENGTH(a.relation_ids) - LENGTH(REPLACE(a.relation_ids,',',''))+2)where a.relation_ids like '%,%' and a.pay_no_arr is null ) 
-- as mm ,
-- fc_corms_cost_pay as fccp 
-- where fccps.id=mm.resultNAME and fccps.cost_pay_id = fccp.id GROUP BY  mm.verify_no,fccp.payment_document) as kk GROUP BY kk.verify_no;


-- 
SELECT verify_code,payno_arry,GROUP_CONCAT(payno_arry) from test_ref_temp GROUP BY verify_code

-- UPDATE fc_corms_cost_verify as a, test_ref_temp as b set a.pay_no_arr = b.payno_arry where a.verify_no =b.verify_code and a.pay_no_arr is null and a.relation_ids like '%,%';
-- SELECT * from  fc_corms_cost_verify as a, test_ref_temp as b where a.verify_no =b.verify_code and a.pay_no_arr is null and a.relation_ids like '%,%';


SELECT * from  fc_corms_cost_verify as a where  a.pay_no_arr is null and a.relation_ids like '%,%';

-- INSERT INTO test_ref_temp (verify_code, payno_arry)
-- SELECT kk.verify_no,GROUP_CONCAT(kk.payment_document)
--  from (SELECT fccps.other_fee_apply_id,fccps.id, mm.verify_no,fccp.pay_no as payment_document  from 
-- fc_corms_other_fee_apply_rent_money as fccps,
-- (SELECT  a.verify_no,a.relation_ids, SUBSTRING_INDEX(SUBSTRING_INDEX(a.relation_ids,',',b.id),',',-1) AS resultNAME  FROM fc_corms_cost_verify a  JOIN  fc_add_canopy  b  ON b.id < (LENGTH(a.relation_ids) - LENGTH(REPLACE(a.relation_ids,',',''))+2)where a.relation_ids like '%,%' and a.pay_no_arr is null ) 
-- as mm ,
-- fc_corms_other_fee_apply as fccp 
-- where fccps.id=mm.resultNAME and fccps.other_fee_apply_id = fccp.id GROUP BY  mm.verify_no,fccp.pay_no) as kk GROUP BY kk.verify_no;



SELECT verify_code,GROUP_CONCAT(payno_arry)  as payno_arry from test_ref_temp GROUP BY verify_code;



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值